views:

87

answers:

2

Hello,

I need to perform asp.net web-service function call via jQuery and pass asp.net application path to it. That's the way I'm trying to do it (code is located within asp.net page, e.g. aspx file):

    var d = "{'str':'<%=System.DateTime.Now.ToString() %>', 'applicationPath':'<%=GetApplicationPath() %>'}";

    $.ajax({ type: "POST",
        url: "http://localhost/testwebsite/TestWebService.asmx/Test",
        data: d,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        },
        success: function (msg) {

        }
    });

That's what GetApplicationPath method looks like:

protected string GetApplicationPath() 
{
    return HttpUtility.HtmlEncode(Request.PhysicalApplicationPath);
}

And here is a header of web-service function which I'm trying to call:

public void Test(string str, string applicationPath) 

Function call works well, but applicationPath parameter doesn't passed correctly. When I debug it I see that backslashes are removed, function gets "C:ProjectsSamplesmytestwebsite" instead of "'C:\Projects\Samples\mytestwebsite\'".

How can I overcome this?

+1  A: 

OK give this a shot, I tried it (works on my machine).

protected string GetApplicationPath()
{
    return Request.PhysicalApplicationPath.Replace(@"\", @"\\");
}

Then for the javascript stuff. Here I'm using the JSON JavaScript library to make escaping things easier.

// construct object literal to be JSON-stringified
var d = {
    str: "<%=System.DateTime.Now.ToString() %>",
    applicationPath: "<%=GetApplicationPath() %>"
};

$.ajax({ type: "POST",
    url: "http://localhost/testwebsite/TestWebService.asmx/Test",
    data: JSON.stringify(d),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    error: function (xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert(err.Message);
    },
    success: function (msg) {

    }
});
Craig
I've tried, it didn't work
the_V
Make sure you have [ScriptService] attribute on the class, and [WebMethod] and [ScriptMethod(ResponseFormat = ResponseFormat.Json)] on the method.
Craig
Perhaps HttpUtility.HtmlEncode(...) is the problem.
Craig
Of course, I have [ScriptService] attribute applied to service class definition and [WebMethod] applied to the method I'm trying to call. Do you think I would say that function call works well if I had not added these attributes? ResponseFormat stuff didn't work.
the_V
Great, works, thanks!
the_V
+1  A: 

Backslashes need to be escaped in JavaScript. Try doing a replace of "\" with double backslashes "\ \" (minus the space):

protected string GetApplicationPath()  
{ 
    return Request.PhysicalApplicationPath.Replace("\", "\\"); 
}

I don't think you need the HTMLEncode part.

BradBrening
You'll need to escape the \ in C# as well. Best to use C# verbatim strings for this. @"\" and @"\\".
Craig
Good catch! I totally missed that.
BradBrening