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
Does your web.config has any HttpHandlers of type System.Web.Script.Services.ScriptHandlerFactory? Make sure that these are for the same version of .NET as your application or remove them if you are using .NET Framework 4 as they are included at machine
level.
Thank you for your suggestion. I applied the configuration you suggested and now I'm getting Server Error 500 as if the web.config is improperly formatted or something.
I'm playing with asmx to explore alternative implementations because until now, I've written my own webservices as blank .aspx pages that simply perform a Response.Write to whichever data I want, formatted exactly as I do it. What are the benefits of using
asmx instead of my method. In the end, I just need a string of characters from the server. Seems like a lot more of a hassle to babysit the framework and account for extraneous information like this or even the "d:" attribute if the webservice was functioning
as expected.
"Dream as if you'll live forever, live as if you'll die today." --James Dean
AceCorban
Star
12594 Points
2318 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.
mm10
Contributor
6455 Points
1187 Posts
Re: No Seriously, Return JSON
Apr 10, 2012 08:20 PM|LINK
Does your web.config has any HttpHandlers of type System.Web.Script.Services.ScriptHandlerFactory? Make sure that these are for the same version of .NET as your application or remove them if you are using .NET Framework 4 as they are included at machine level.
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
AceCorban
Star
12594 Points
2318 Posts
Re: No Seriously, Return JSON
Apr 10, 2012 09:01 PM|LINK
Thank you for your suggestion. I applied the configuration you suggested and now I'm getting Server Error 500 as if the web.config is improperly formatted or something.
I'm playing with asmx to explore alternative implementations because until now, I've written my own webservices as blank .aspx pages that simply perform a Response.Write to whichever data I want, formatted exactly as I do it. What are the benefits of using asmx instead of my method. In the end, I just need a string of characters from the server. Seems like a lot more of a hassle to babysit the framework and account for extraneous information like this or even the "d:" attribute if the webservice was functioning as expected.
mm10
Contributor
6455 Points
1187 Posts
Re: No Seriously, Return JSON
Apr 11, 2012 07:23 AM|LINK
How does your web.config look like?
AceCorban
Star
12594 Points
2318 Posts
Re: No Seriously, Return JSON
Apr 11, 2012 05:37 PM|LINK
When I tested it, it looked like this:
<?xml version="1.0"?> <configuration> <connectionStrings> [MyConnectionString] </connectionStrings> <system.web> <httpHandlers> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </httpHandlers> <compilation debug="true" targetFramework="4.0"/> </system.web> </configuruation>I also tried a "remove" tag before the one you suggested to remove all verbs, but that didn't seem to change anything either.
mm10
Contributor
6455 Points
1187 Posts
Re: No Seriously, Return JSON
Apr 11, 2012 06:34 PM|LINK
And on IIS7 you get a 500 server error? Or XML data back from the function instead of JSON?
AceCorban
Star
12594 Points
2318 Posts
Re: No Seriously, Return JSON
Apr 11, 2012 06:45 PM|LINK
With the web.config I showed you above, running on IIS7, I get a 500 error.
With the web.config I showed you minus the httpHandler nodes, running on IIS7, I get XML encasing my JSON data.
Running it through Visual Studio on my development machine, I don't even need the web.config changes and it returns JSON as expected.
mm10
Contributor
6455 Points
1187 Posts
Re: No Seriously, Return JSON
Apr 11, 2012 07:18 PM|LINK
You get an internal server error because you are not supposed to have httpHandlers inside <system.web> when running in integrated mode in IIS 7.
Try this configuration:
<configuration> <connectionStrings> [MyConnectionString] </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0"/> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules> <remove name="ScriptModule"/> <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </modules> <handlers> <remove name="WebServiceHandlerFactory-Integrated"/> <remove name="ScriptHandlerFactory"/> <remove name="ScriptHandlerFactoryAppServices"/> <remove name="ScriptResource"/> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </handlers> </system.webServer> </configuruation>