Asmx web service returns xml instead of json in .net 4.0

I just updated a test copy of my site to asp.net 4.0 and noticed a strange problem that only occurs when I upload the site to my server.

the site has an asmx web service that returns json, but when I start the site on my server, it returns xml. It has been working fine in asp.net 3.5 for over a year.

webMethod is decorated with the right attributes ...

[WebMethod][ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<LocationRecentChange> RecentChanges() 

and on my local machine it returns json.

still on the server (Windows 2008 64bit) it returns xml.

using the firebug console, you will see a 200 OK response and a bunch of XML, and on my local machine, the returned data is the expected JSON.

Here is the javascript that calls the service.

 function loadRecentData() { $.ajax({ type: "POST", url: "service/spots.asmx/RecentChanges", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: loadRecentUpdates, failure: function(msg) { //alert(msg); } }); 

}

Any suggestions are welcome, it puzzled me!

+8
json jquery c # web-services
May 01 '10 at 12:45
source share
2 answers

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

A string like "ScriptHandlerFactory" in .NET 4:

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

Now ASP.NET 4 includes machine-level default web.config. Thus, you do not need to map the ScriptHandlerFactory in the web.config project, 3.5 or 4.

+7
May 05 '10 at 15:32
source share

Well, I managed to track it by reading that ajax web service requests are being processed here ...

http://www.asp.net/%28S%28ywiyuluxr3qb2dfva1z5lgeg%29%29/learn/ajax/tutorial-05-cs.aspx

basically, the handler from asp.net 3.5 should be declared in the handlers section of web.config in the system.webserver file so that it can return a JSON response instead of the standard one.

here is what you need to add to the web.config handlers section (also add to httpHandlers if you need to support IIS6) ...

 <handlers> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 

I have a suspicion that this will require replacing the .net 4.0 version of the same handler, but at the moment it works.

+2
May 01 '10 at 14:10
source share



All Articles