I've read many posts on this and they all suggest what I am already doing:
jQuery javascript:
var responseText = $.ajax(
{
type: "POST",
contentType: "application/json",
url: "WebServices/MyServices.asmx/GetJSON",
data: "{}",
success: function(msg)
{
if(msg.d)
{
var myObj = $.parseJSON(msg.d);
//do stuff with myObj
}
}
});
Note, I am explicitly specifying both POST and contentType application/json.
In the asmx:
[WebMethod]
[ScriptMethod(UseHttpGet=false, ResponseFormat=ResponseFormat.Json)]
public string GetJSON()
{
return "{\"testData\":\"Whatever JSON Data I want\"}";
}
Note, I am explicitly specifiying ResponseFormat correctly and indicating that it will not use GET.
When I run it within Visual Studio, it works as expected, returning JSON-formatted data with the annoying "d:" attribute
{"d":"{"testData":"Whatever JSON data I want"}"}
and my javascript works just fine accessing the actual JSON data.
When I deploy this to an II7 server, however, it comes back as xml:
<?xml version="1.0" encoding="utf-8"?>
<string xmnls="http://myDomain.org/">{"testData":"Whatever JSON Data I want"}</string>
Seems like a configuration issue or something.
"Dream as if you'll live forever, live as if you'll die today." --James Dean
AceCorban
Star
12344 Points
2272 Posts
No Seriously, Return JSON
Apr 10, 2012 06:10 PM|LINK
I've read many posts on this and they all suggest what I am already doing:
jQuery javascript:
var responseText = $.ajax( { type: "POST", contentType: "application/json", url: "WebServices/MyServices.asmx/GetJSON", data: "{}", success: function(msg) { if(msg.d) { var myObj = $.parseJSON(msg.d); //do stuff with myObj } } });Note, I am explicitly specifying both POST and contentType application/json.
In the asmx:
[WebMethod] [ScriptMethod(UseHttpGet=false, ResponseFormat=ResponseFormat.Json)] public string GetJSON() { return "{\"testData\":\"Whatever JSON Data I want\"}"; }Note, I am explicitly specifiying ResponseFormat correctly and indicating that it will not use GET.
When I run it within Visual Studio, it works as expected, returning JSON-formatted data with the annoying "d:" attribute
{"d":"{"testData":"Whatever JSON data I want"}"}and my javascript works just fine accessing the actual JSON data.
When I deploy this to an II7 server, however, it comes back as xml:
<?xml version="1.0" encoding="utf-8"?> <string xmnls="http://myDomain.org/">{"testData":"Whatever JSON Data I want"}</string>Seems like a configuration issue or something.