ASP.NET Web Service erroneously returns XML instead of JSON

I am trying to use the ASMX web service from javascript using jQuery. It works fine when I ask for XML, but I want to use .net JSON; (it also starts to bother me that this is not working)

Web Service Code:

using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Script.Services; [WebService()] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class SimpleService : WebService { [WebMethod] public String GetString() { return "value"; } } 

Customer Code:

 $.ajax({ type: "POST", url: "SimpleService.asmx/GetString", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json" }); 

And the answer is ...

 Content-Type: text/xml; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET <?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">value</string> 

The query always succeeds, but jQuery gives me an analyzer error (which is not surprising given the answer).

I'm crazy. I tried adding the ServiceMethod attribute with the ResponseType parameter set to JSON, but nothing works.

I do not want to use the javascript.NET ScriptManager generator, so please do not suggest using them.

+3
json web-services asmx asp.net-ajax
Jan 30 2018-11-11T00:
source share
3 answers

This is a 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 ScriptHandlerFactory for IIS6, and I used IIS7. Adding the httpHandler mapping to the IIS7 section in web.config solved the problem.

I hate hidden moving parts ....

+2
Jan 30 '11 at 9:13
source share

No answer to SO helped me solve this problem. Instead, I found 2 articles describing this problem.

jQuery does not encode query data in JSON, but into a query string. This causes ASP.NET to ignore the Accept header and respond with XML.

Mark this article under the heading "JSON, Objects, and Strings: oh my!".

Here I quote:

  $ .ajax ({
     type: "POST",
     url: "WebService.asmx / WebMethodName",
     data: {'fname': 'dave', 'lname': 'ward'},
     contentType: "application / json; charset = utf-8",
     dataType: "json"
 }); 

Because this data parameter is a valid object literal, calling the web service in this way does not cause client-side JavaScript errors. Unfortunately, he will not have the desired result either.

 fname=dave&lname=ward 

This is clearly not what we want. The solution is to make sure you pass the jQuery string to the data parameter, for example:

  $ .ajax ({
     type: "POST",
     url: "WebService.asmx / WebMethodName",
     data: "{'fname': 'dave', 'lname': 'ward'}",
     contentType: "application / json; charset = utf-8",
     dataType: "json"
    }); 

A slight change in the syntax, but it matters. Now jQuery will leave our JSON object on its own and pass the entire string to ASP.NET for server-side parsing.

In my case, the data parameter is a large object, so I use something like this to serialize it to JSON manually.

 data: JSON.stringify({'fname':'dave', 'lname':'ward'}), 

Getting an ASP.NET ScriptService to return JSON when querying from jQuery is very difficult, and many parameters in your code can make it throw XML instead of JSON. You must read the various SO Q / A to be satisfied.

A related article forms the same author who can give more recommendations.

+3
Sep 18 '13 at 13:39 on
source share

Try adding to the [ScriptMethod] method:

 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public String GetString() 
0
Jan 30 '11 at 9:02
source share



All Articles