I am trying to write an ASP.net web service that will be used by a jQuery AJAX call. I'm completely out of my mind trying to get this to work. I saw a number of similar questions on the Internet, but I could not find a solution that fits.
When I try to make an ajax call (via jquery), I get a successful response from the server, but the request fails due to a parser error.
I checked the json returned by the webservice and it is valid. The problem seems to be due to asp.net returning a json object as xml.
I have specified return type as json using
<Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
I added the following http handler, as mentioned as a potential fix.
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
"application/json; charset = utf-8" dataType "json" jQuery ajax. , xml.
, dataType, eval .
- - , . .
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() {
$.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;
}
};
}) (Jquery);//jQuery Alias Block
ASP.NET Webservice
<%@ WebService Language="VB" Class="SpeciesService" %>
System.Web
System.Web.Services
System.Web.Services.Protocols
System.Runtime.Serialization
' - script, ASP.NET AJAX, .
'_
_
_
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