views:

2645

answers:

4

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

+1  A: 

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.

Rashack
+2  A: 

Try posting a dummy json data with JQuery like this :

$.ajaxSetup({
    type: "POST",
    data : "{'foo':'bar'}"
...
çağdaş
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.
Dave Ward
+1  A: 

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.

jayrdub
A: 

Alright - Thank you guys very much for your help. The lack of dummy data was the core of the problem. When I tried to add it before it caused a server error and I moved on to something else.

I can't even figure out what fixed the server error portion of the problem. I just spent ten minutes absentmindedly messing with syntax (fixing indents, caps, comments) because i was getting too frustrated to focus on the problem at hand. I refreshed the page and suddenly it is working fine. So the answer was to post dummy data and fix random mystery syntax error.

Anyways I can not thank you enough for getting me back on the right track.

apocalypse9