Ok, so I have a JQuery AJAX call in an .html file, sitting on my desktop. It makes a call to a WCF Service located on a web server, out there in the ether.
The good news is that it works. Sorta.. almost.. but not entirely.
The AJAX call will execute, and Fiddler shows a JSON Response from the web service. Unfortunately it doesn’t get all the way back to the browser. The callback=? is there so I don’t get a cross-site scripting error. (Access-Control blah blah blah)
If I do it with Chrome’s development tools open it gives me the following message:
Resource interpreted as script but transferred with MIME type application/json
The success: callback isn’t called, and the error: callback returns a single word in the statusText: parsererror
I’ve been banging my head on this all day.
function AJAXGet() {
$.ajax({
url: "http://xxxx/yyyyService/yyyyService.svc/GetZZZ?callback=?",
dataType: 'json',
data: JSON.stringify('{"customerID": "1"}'),
contentType: "application/json; charset=utf-8; application/x-javascript",
success: function (data) {
alert(data);
},
complete: function (data) {
alert(data);
},
error: function (xhr) {
alert("AJAXGetError: " + xhr.statusText);
}
});
}
Fiddler says this is what was sent:
GET /yyyyService/yyyyService.svc/GetZZZ?callback=jQuery150833801421286173_1297309350515&%22{\%22yyyyID\%22:%20\%221\%22}%22&_=1297309350540 HTTP/1.
Here’s the WCF:
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<Note> GetNotes(string yyyyID)
You are using JSONP – your server side code must support it. In this case, your WCF service should be returning a script-let such as
[callback name]([json string])where [callback name] would be jquery generated js function (what you see as callback value in fiddler request) and [json string] would your usual JSON data from service. So check the response in fiddler. If your service is not returning JSONP response then you need to add that support at service end.Check this article for how to do it in .NET 4 (that has support for JSONP). For .NET 3.5, its more work – see this MSDN article for extending WCF to supprot JSONP. Yet another quick hack (for .NET 3.5) could be to build HttpModule that will watch JSON request and modify the response if the request url has
callbackparameter.