I have a simple .NET [asmx] web service setup like this:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public string Find(string stateAbbrev, string city, string name)
{
// Code
}
I call this web method from this:
$.ajax({
type: "get",
contentType: "application/json; charset=utf-8",
cache: false,
url: "/webservice.asmx/Find",
data: { stateAbbrev: "'" + escape(self.selectedState()) + "'", city: "'" + escape(self.city()) + "'", name: "'" + escape(self.name()) + "'" },
dataType: "json",
success: function() { }
});
I’ve tried several variations with the above url/data parameters. Including encodeURI, encodeURIComponent and escape. The problem is that the data encoded on the client side is not being handled on the server properly. What I mean is that when I encode/escape the query strings, I end up with a HTTP GET request like this:
http://localhost/webservice.asmx/Find?stateAbbrev=''&city='MyCity%2527'&name=''
The %2527 is an escaped character. The server handles this that the city name is actually “MyCity%2527”.
Do I need to do anything on the server itself, or should the decoding of query strings be handled by .NET? Am I doing the encoding correctly?
Thank You,
Unfortunately, the only way I could get around this was to use a HTTP ‘POST’ instead of a ‘GET’, since POSTing will automatically encode the data sent to the server.