4

I'm attempting to use an ASMX web service from javascript using jQuery. It works fine when I ask for XML, but I want to make use of .net's JSON serialization functionality; (it's also starting to bug me that this isn't working)

The code for the web service:

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";
 }
}

The code for the client:

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

And the response...

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 request always succeeds, but jQuery gives me a parser error (not surprisingly, given the response).

I'm at my wits end. I've tried adding a ServiceMethod attribute with the ResponseType set to JSON, but nothing seems to work.

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

John Saunders
162k26 gold badges252 silver badges403 bronze badges
asked Jan 30, 2011 at 8:53

3 Answers 3

3

No answer on SO helped me solve this issue. Instead I found 2 articles describing this problem.

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

Check this article at the title "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 that data parameter is a valid object literal, calling the web service this way won’t throw any JavaScript errors on the client-side. Unfortunately, it won’t have the desired result either.

fname=dave&lname=ward

This is clearly not what we want to happen. The solution is to make sure that you’re passing jQuery a string for the data parameter, like this:

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

It’s a small change in syntax, but makes all the difference. Now, jQuery will leave our JSON object alone and pass the entire string to ASP.NET for parsing on the server side.

In my case the data parameter is a big object so I use something like that to serialize it to JSON manualy.

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

Getting ASP.NET ScriptService to return JSON when querying from jQuery is very tricky and many parameters in your code can make it throw XML instead of JSON. You should read various SO Q/A to get yourself satisfied.

Related article form the same author that may give more guidance.

answered Sep 18, 2013 at 13:39
2

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....

answered Jan 30, 2011 at 9:13
0

Try adding the [ScriptMethod] attribute to the method:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public String GetString()
answered Jan 30, 2011 at 9:02
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.