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.
我正在尝试编写一个ASP.net web服务,它将被jQuery AJAX调用使用。我实在是绞尽脑汁想把这事办好。我在网上看到过很多类似的问题,但我一直没有找到合适的解决方案。
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.
当我尝试使用ajax调用(通过jquery)时,我从服务器获得了一个成功的响应,但是由于解析器错误,请求失败了。
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.
我已经验证了webservice返回的json,它是有效的。这个问题似乎源于asp.net将json对象作为xml返回的事实。
I've specified the return type as json using
我使用了json指定了返回类型。
<Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
I've added the following http handler as it was mentioned as a potential fix
我添加了以下http处理程序,正如前面提到的,它是一个潜在的修复
<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.
内容类型设置为“application/json;在jquery ajax设置中,charset=utf-8和“json”的数据类型。请求类型似乎是正确的,但响应始终是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.
我可以通过删除数据类型成功地发出请求,但是我非常希望避免使用eval来反序列化数据。
If anyone has any suggestion i will be greatly appreciated. I've been pulling my hair out for a few days on this.
如果有人有任何建议,我将非常感激。这件事我已经紧张了好几天了。
JAVASCRIPT
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
})(jQuery);/ / jQuery别名
ASP.NET Webservice
ASP。网网络服务
<%@ WebService Language="VB" Class="SpeciesService" %>
Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols Imports Species Imports System.Runtime.Serialization
导入系统。网络包含进口。System.Web.Services进口服务。协议导入物种的System.Runtime.Serialization
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. ' _
使用ASP从脚本中调用这个Web服务。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
End Class
结束课
4 个解决方案
#1
2
Try posting a dummy json data with JQuery like this :
尝试使用JQuery发布一个假json数据:
$.ajaxSetup({
type: "POST",
data : "{'foo':'bar'}"
...
#2
1
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.
这更多的是暗示而不是答案——我使用的是PageMethods而不是webservices,而且效果很好。如果它也有同样的问题,我就试试。
#3
1
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.
如果你看标题,是否有一个内容长度被发送?IIS需要一个post的内容长度,因此即使在post中添加一个空的body,也可能会修复它。
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.
我认为它使用json数据类型的原因是jquery为您添加了一个空的post body。
#4
0
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.
我甚至不能确定问题的服务器错误部分是什么。我只是心不在焉地花了10分钟来修改语法(修改缩进、大写、注释),因为我太沮丧了,无法专注于手头的问题。我刷新了页面,突然它工作得很好。所以答案是发布假数据并修复随机的神秘语法错误。
Anyways I can not thank you enough for getting me back on the right track.
无论如何,你让我回到了正确的道路上,我感激不尽。
#1
2
Try posting a dummy json data with JQuery like this :
尝试使用JQuery发布一个假json数据:
$.ajaxSetup({
type: "POST",
data : "{'foo':'bar'}"
...
#2
1
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.
这更多的是暗示而不是答案——我使用的是PageMethods而不是webservices,而且效果很好。如果它也有同样的问题,我就试试。
#3
1
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.
如果你看标题,是否有一个内容长度被发送?IIS需要一个post的内容长度,因此即使在post中添加一个空的body,也可能会修复它。
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.
我认为它使用json数据类型的原因是jquery为您添加了一个空的post body。
#4
0
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.
我甚至不能确定问题的服务器错误部分是什么。我只是心不在焉地花了10分钟来修改语法(修改缩进、大写、注释),因为我太沮丧了,无法专注于手头的问题。我刷新了页面,突然它工作得很好。所以答案是发布假数据并修复随机的神秘语法错误。
Anyways I can not thank you enough for getting me back on the right track.
无论如何,你让我回到了正确的道路上,我感激不尽。