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