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.Se开发者_C百科rvices.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.
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.
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....
Try adding the [ScriptMethod]
attribute to the method:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public String GetString()
精彩评论