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.