Calling Remote ASP.NET Web Services from JQuery
This is Jason masquerading as a guest. I really should post more often…
I spent the best part of a day banging my head against the wall with this problem and I thought that I’d share the solution that I came up with. I’m sure that there’s a better way to do this and if there is I’d love to know what it is. And of course there are other viable approaches such as calling a local Web Service that would forward the request onto the remote server. But I am extremely stubborn and I HATE losing (both a good and a bad quality and probably worthy of a post in it’s own right) so once I started down this path I was not about to give up without a fight.
What I wanted to do is trivial against when interacting with ASP.NET Web Services on a local server using JSON rather than XML. A great post by David Ward explains how to do this. But when I tried this approach against a different domain it failed. Bob Ippolito explains the problem in his original proposal for the JSONP standard. Now JQuery supports JSONP so I thought that this would be easy from here but I was wrong.
I hooked up the following Javascript to a button click event on a test page:
function test() {
$.ajax({ url: http://remoteserver/web_service.asmx/TestMethod”,
data: { ID: 1 },
dataType: “jsonp”,
success: function(json) {
alert(json.d);
}
});
}
The call was successful but the returned data was XML not JSON:
<string xmlns=”http://tempuri.org/”>abc</string>
This resulted in a ‘missing ; before statement’ error when JQuery tried to parse the data. After doing some research and looking at the difference is the requests generated by the JSON and JSONP dataTypes I realized that the issue was that the correct Content-Type was not being set by JQuery. This is required by ASP.NET Web Services as as explained here by Scott Guthrie. Rather than take on the challenge in the JQuery source I added a simple HttpModule to the remote server that sets the Content-Type to ‘application/json; charset=utf-8′ for remote requests.
After doing this I was finally receiving JSON:
{“d”:”abc”}
But I was now seeing an ‘Invalid label’ error.
After some more Googling I stumbled across this article by T Bogard. The salient point is that I had to append the name of the callback function to the returned JSON so that the browser knows what code to execute when it receives the response. Basically ASP.NET Web Services are, to the best of my knowledge, not JSONP aware. I already had an HttpModule in play and so I figured that would be the best place to take care of the wrapping. A post by Phil Hack explained how to modify the HttpResponse. Now the returned JSON looks like this:
jsonp1227742793574({“d”:”abc”});
Finally the message box is displayed with the correct data…and I can finally get some sleep.


