9

i have just upgraded a test copy of my site to asp.net 4.0 and have noticed a strange issue that only arises when i upload the site to my server.

the site has an asmx web service that returns json, yet when i run the site on my server it returns xml. it as been working fine in asp.net 3.5 for over a year.

the webMethod is decorated with the correct attributes...

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

and on my local machine it returns json.

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

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

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 welcome, this has got me stumped!

Baldy
  • 3,621
  • 4
  • 38
  • 60

2 Answers2

7

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.

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
  • Turns out the server has something funky going on with IIS. I cannot recreate the issue locally, but inserting the item into the web.config does fix it. – Baldy May 05 '10 at 19:38
  • 3
    I am having exactly the same problem. Can you edit the answer to include the exact code I need to add to my web.config (including which section it needs to be added to)? Thanks. – njr101 Jun 19 '12 at 07:25
2

well i managed to track it down by reading up about ajax web services requests are handled here...

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

basically a handler from asp.net 3.5 needs to be declared in your web.config handlers section under system.webserver so that it can return a JSON response instead of the default.

here is what you need to add to the web.config handlers section (also add to httpHandlers section 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 my suspicions that this will need replacing with a .net 4.0 version of the same handler, but for now, it works.

Baldy
  • 3,621
  • 4
  • 38
  • 60
  • 1
    I am having exactly the same problem. Can you edit the answer to include the include which section it needs to be added to? And any additional elements such as if required. Thanks. – njr101 Jun 19 '12 at 07:27
  • have updated it for you and added links to relevant section documentation – Baldy Jun 19 '12 at 10:51
  • But you haven't said where to add the section... Where does that go? Where ever I put it, I get an error. – NickG Apr 11 '13 at 14:51
  • It goes in the system.webServer section of the web.config – Baldy Apr 11 '13 at 19:27