Problems with ASP.net JSON Webservice response type

10,149

Solution 1

Try posting a dummy json data with JQuery like this :

$.ajaxSetup({
    type: "POST",
    data : "{'foo':'bar'}"
...

Solution 2

This is more hint than an answer - I've been using PageMethods rather than webservices and it works very well. I'd try that if it suffers with the same issue.

Solution 3

If you watch the headers is there a content-length being sent? IIS requires a content-length on a post, so even adding an empty body to the post might fix it.

And I think the reason it works with the datatype set to json is that jquery adds an empty post body for you in that case.

Share:
10,149
Kelly Robins
Author by

Kelly Robins

...

Updated on June 05, 2022

Comments

  • Kelly Robins
    Kelly Robins almost 2 years

    I am attempting to write an ASP.net web service that will be utilized by a jQuery AJAX call. I am absolutely at my wit's end trying to get this to work. I've seen a number of similar questions online but I haven't been able to find a solution that fits.

    When I attempt to make the ajax call (via jquery) I get a successful response from the server but the request fails because of a parser error.

    I've validated the json returned by the webservice and it is valid. The issue seems to stem from the fact that asp.net is returning the json object as xml.

    I've specified the return type as json using

    <Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
    

    I've added the following http handler as it was mentioned as a potential fix

    <httpHandlers>
        <remove verb="*" path="*.asmx" />
        <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false" />
    </httpHandlers>
    

    The content Type is set to "application/json; charset=utf-8" and the dataType to "json" in my jquery ajax setup. The request type seems to be correct but the response is always xml.

    I can make the request successfully by removing the dataType but i would very much like to avoid using an eval to deserialize the data.

    If anyone has any suggestion i will be greatly appreciated. I've been pulling my hair out for a few days on this.


    JAVASCRIPT

    (function($) {
      $.ajaxSetup({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        global: false,
        dataType: "json"
      });
    
      function auto() {
        console.log("test");
        return;
      };
    
      $.fn.species = {
        test: function() {
          alert("test");
        },
        load: function() { //load and attach species list to element as dropdown
          $.ajax({
            url: "SpeciesService.asmx/List",
            success: function(msg) {
              console.log(msg);
            },
            error: function(xhr, desc, exceptionobj) {
              console.log(xhr.responseText);
              console.log(desc);
              console.log(exceptionobj);
            }
          });
          return this;
        }
      }; //Species Block
    })(jQuery); //jQuery Alias Block
    

    ASP.NET Webservice

    <%@ WebService Language="VB" Class="SpeciesService" %>
    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports Species
    Imports System.Runtime.Serialization
    

    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. ' _

    _ _ Public Class SpeciesService

    Inherits System.Web.Services.WebService
    <WebMethod()> _
    <Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
    Public Function Data(ByVal id As Integer) As String
        Dim curSpecies As New Species(id)
        Return curSpecies.serialize
    End Function
    
    <WebMethod()> _
    <Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
    Public Function List() As String
    
        Return Species.list()
    End Function
    

    End Class

  • rlorenzo
    rlorenzo about 14 years
    I believe this issue is fixed in jQuery 1.4.x. In jQuery 1.3.x, all you need is a data: '{}'. Any data will force jQuery to set the content-type; legitimate keys and values aren't necessary.