ASP.NET Asmx Web Service Returning Xml Instead of JSON

asp.net asmx web service returning xml instead of json

Finally figured it out.

The app code is correct as posted. The problem is with the configuration. The correct web.config is:

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<handlers>
<add name="ScriptHandlerFactory"
verb="*" path="*.asmx"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>

According to the docs, registering the handler should be unnecessary from .NET 4 upwards as it has been moved to the machine.config. For whatever reason, this isn't working for me. But adding the registration to the web.config for my app resolved the problem.

A lot of the articles on this problem instruct to add the handler to the <system.web> section. This does NOT work and causes a whole load of other problems. I tried adding the handler to both sections and this generates a set of other migration errors which completely misdirected my troubleshooting.

In case it helps anyone else, if I had ther same problem again, here is the checklist I would review:

  1. Did you specify type: "POST" in the ajax request?
  2. Did you specify contentType: "application/json; charset=utf-8" in the ajax request?
  3. Did you specify dataType: "json"in the ajax request?
  4. Does your .asmx web service include the [ScriptService] attribute?
  5. Does your web method include the [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    attribute? (My code works even without this attribute, but a lot of articles say that it is required)
  6. Have you added the ScriptHandlerFactory to the web.config file in <system.webServer><handlers>?
  7. Have you removed all handlers from the the web.config file in in <system.web><httpHandlers>?

Hope this helps anyone with the same problem. and thanks to posters for suggestions.

ASP.NET web service erroneously returns XML instead of JSON

This one is user error.

I just stumbled upon this other stackoverflow question: web-service returning xml instead of json in net 4-0

A similar solution turned out to be what I needed. The web.config file had an httpHandler mapping for the ScriptHandlerFactory for IIS6, and I was using IIS7. Adding the httpHandler mapping to the IIS7 section of the web.config solved the problem.

I hate hidden moving parts....

asmx web service returning xml instead of json in .net 4.0

Are you sure .NET 4 is installed on your server?

The ScriptHandlerFactory's "type" string in .NET 4 is:

System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

ASP.NET 4 now includes that in its default web.config at the machine level. So, you shouldn't need any mapping to the ScriptHandlerFactory in your project's web.config, 3.5 or 4.

ASP.NET web service returning xml instead of json

Probably it's because you are used to VB being case-insensitive.

Try these lines (note the capitol T):

 contentType: 'application/json; charset=utf-8',
dataType: 'json',


Related Topics



Leave a reply



Submit