asp.net asmx web service returning xml instead of json asp.net asmx web service returning xml instead of json asp.net asp.net

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.


No success with above solution, here how I resolved it.

put this line into your webservice and rather return type just write the string in response context

this.Context.Response.ContentType = "application/json; charset=utf-8";this.Context.Response.Write(serial.Serialize(city));


If you want to stay remain with Framework 3.5, you need to make change in code as follows.

[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [ScriptService]public class WebService : System.Web.Services.WebService{    public WebService()    {    }    [WebMethod]    public void HelloWorld() // It's IMP to keep return type void.    {        string strResult = "Hello World";        object objResultD = new { d = strResult }; // To make result similarly like ASP.Net Web Service in JSON form. You can skip if it's not needed in this form.        System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();        string strResponse = ser.Serialize(objResultD);        string strCallback = Context.Request.QueryString["callback"]; // Get callback method name. e.g. jQuery17019982320107502116_1378635607531        strResponse = strCallback + "(" + strResponse + ")"; // e.g. jQuery17019982320107502116_1378635607531(....)        Context.Response.Clear();        Context.Response.ContentType = "application/json";        Context.Response.AddHeader("content-length", strResponse.Length.ToString());        Context.Response.Flush();        Context.Response.Write(strResponse);    }}