Skip to content Skip to sidebar Skip to footer

Ajax Query To My Webservices Is Returning Xml In My Json

I am having this exact problem as raised in this question ASP.NET JSON web service always return the JSON response wrapped in XML It's late and I have been sitting in this chair fo

Solution 1:

If you set the response type to jsonjQuery is then checking the response to see if it's valid JSON (and since it's XML, that's not the case)...when it's not valid, it silently fails since jQuery 1.4+.

There are 3 important bits when making your request, by default it needs to be a POST and you need to set the contentType to "application/json; charset=utf-8" like this:

$.ajax({
  url: 'MyService.asmx/Method',
  type: 'POST',
  data: myData,
  dataType: 'json',
  contentType: "application/json; charset=utf-8",
  success: function(data) {
    //do something with data
  }
});

Then on the server-side make sure you have the ScriptService attribute set, here's an example very minimal layout:

[WebService] //here by default when you create the service
[ScriptService]
publicclassMyService : System.Web.Services.WebService 
{
  [WebMethod]
  public MyObject MyMethod(int id) 
  {
    returnnew MyObject();
  }
}

Post a Comment for "Ajax Query To My Webservices Is Returning Xml In My Json"