Please look at the code below and help me to figure out what am I doing wrong in my web service code. I want to set up an web service that can be consumed using an JSONP. I am using Jquery at client side to access the site. Even after setting up proper attributes my web service still emits xml due to which the aynch call fails.

Web Service

[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 

    [ScriptMethod(ResponseFormat= ResponseFormat.Json, XmlSerializeString=false, UseHttpGet=true)]
    public string HelloWorld(int id, string __callback) {
        return  __callback + "({message: 'Hello World'})";


Web Service Response:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns=""&gt;test({message: 'Hello World'})</string>


     <add name="HttpGet"/>
     <add name="HttpPost"/>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" 
      System.Web.Extensions, Version=, 
      Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="*" path="*_AppService.axd" validate="false" 
      System.Web.Extensions, Version=, 
      Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="GET,HEAD" path="ScriptResource.axd" 
      System.Web.Extensions, Version=, 
      Culture=neutral, PublicKeyToken=31BF3856AD364E35" 
    <add name="ScriptModule" 
     type="System.Web.Handlers.ScriptModule, System.Web.Extensions, 
      Version=, Culture=neutral, 


    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://localhost:54404/jsonp/webservice.asmx/HelloWorld?id=2&amp;__callback=?", //?jsonp=MatchSvcCallback
    dataType: "jsonp",
    data: {},
    //jsonp : "MatchSvcCallback",
    success: function(msg) {
    alert("Inside success callback function"); // not being called

    error: function(xhr, msg){
        var response = JSON.parse(xhr.responseText);



The js code works with an handler, but fails for the webservice due to xml response.

+4  A: 

I think the problem here is because jQuery doesn't set the Content-Type header in the HTTP request for HTTP GET's when using $.ajax(). It only gets sent if the request type is 'POST'. This seems to be the case for both dataType: "jsonp" and dataType: "json".

I tried your example and watched the request/response exchange in Fiddler and sure enough I don't see Content-Type: application/xml; charset=UTF-8 being sent in the headers for HTTP GET requests. The header does get sent if using HTTP POST. I'm guessing the presence of this header is a cue for the ASP.NET web service plumbing to decide whether to return either a JSON or XML formatted response.

It seems you're not the only one with this problem, see the following article on the on Elegant Code website:

Calling Remote ASP.NET Web Services from JQuery

The solution appears to be one of the following:

  • Use a HttpModule to inject the Content-Type: application/xml; charset=UTF-8 header into the request stream as described in the article above.

  • Use a HttpHandler for the endpoint as you explained you were doing prior to using an ASP.NET web service.

  • Try Rick Strahl's page based approach in the following article (which is in effect a HttpHandler anyway): JSONP for cross-site Callbacks.


I fixed the content type problem with beforeSent method of the $.ajax, and I cross checked the request headers with firebug they are properly added.
I think i have to add a web service extension to change the response so that the callback method name is added to the response in order to resolve the "invalid label" error from jquery