Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.net JSON Web服务响应类型存在问题_Asp.net_Jquery_Web Services_Json_Asmx - Fatal编程技术网

ASP.net JSON Web服务响应类型存在问题

ASP.net JSON Web服务响应类型存在问题,asp.net,jquery,web-services,json,asmx,Asp.net,Jquery,Web Services,Json,Asmx,我正在尝试编写一个ASP.NETWeb服务,jQueryAjax调用将使用该服务。 要想让这件事成功,我真是束手无策。我在网上看到过很多类似的问题,但我还没有找到一个合适的解决方案 当我尝试进行ajax调用(通过jquery)时,我从服务器获得了一个成功的响应,但由于解析器错误,请求失败 我已经验证了webservice返回的json,它是有效的。这个问题似乎源于asp.net将json对象作为xml返回的事实 我已经使用 <Script.Services.ScriptMethod(res

我正在尝试编写一个ASP.NETWeb服务,jQueryAjax调用将使用该服务。 要想让这件事成功,我真是束手无策。我在网上看到过很多类似的问题,但我还没有找到一个合适的解决方案

当我尝试进行ajax调用(通过jquery)时,我从服务器获得了一个成功的响应,但由于解析器错误,请求失败

我已经验证了webservice返回的json,它是有效的。这个问题似乎源于asp.net将json对象作为xml返回的事实

我已经使用

<Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
ASP.NET Web服务

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

导入系统.Web
导入System.Web.Services
导入System.Web.Services.Protocols
进口物种
导入System.Runtime.Serialization
'要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。 "_

_ _ 公共类物种服务

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
继承System.Web.Services.WebService
_
_
公共函数数据(ByVal id为整数)为字符串
作为新物种的暗草物种(id)
返回curSpecies.serialize
端函数
_
_
作为字符串的公共函数List()
返回物种列表()
端函数

最终类

这与其说是答案,不如说是提示——我一直在使用PageMethods,而不是webservices,而且效果很好。如果遇到同样的问题,我会尝试这样做。

尝试用JQuery发布一个伪json数据,如下所示:

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

如果您查看标题,是否发送内容长度?IIS要求文章的内容长度,因此即使在文章中添加一个空正文也可能会解决此问题


我认为它与设置为json的数据类型一起工作的原因是jquery在这种情况下为您添加了一个空的帖子正文。

好的,非常感谢大家的帮助。 缺乏虚拟数据是问题的核心。当我试图在它导致服务器错误之前添加它时,我转到了其他内容

我甚至不知道是什么修复了问题的服务器错误部分。我只是花了十分钟心不在焉地处理语法(修正缩进、大写、注释),因为我太沮丧了,无法专注于手头的问题。我刷新了页面,突然它工作正常。所以答案是发布虚拟数据并修复随机的神秘语法错误


不管怎样,我还是非常感谢你让我回到正确的轨道上

我相信这个问题在jQuery1.4.x中已经解决了。在jQuery1.3.x中,您只需要一个数据:{}。任何数据都会强制jQuery设置内容类型;合法的键和值不是必需的。
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
$.ajaxSetup({
    type: "POST",
    data : "{'foo':'bar'}"
...