为什么这个简单的 Web 服务拒绝向客户端返回 JSON?
这是我的客户端代码:
var params = { };
$.ajax({
url: "/Services/SessionServices.asmx/HelloWorld",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
timeout: 10000,
data: JSON.stringify(params),
success: function (response) {
console.log(response);
}
});
还有服务:
namespace myproject.frontend.Services
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SessionServices : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
}
}
web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
然后回应:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
无论我做什么,响应总是以 XML 形式返回。如何让网络服务返回 Json?
编辑:
这是 Fiddler HTTP 跟踪:
REQUEST
-------
POST http://myproject.local/Services/SessionServices.asmx/HelloWorld HTTP/1.1
Host: myproject.local
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://myproject.local/Pages/Test.aspx
Content-Length: 2
Cookie: ASP.NET_SessionId=5tvpx1ph1uiie2o1c5wzx0bz
Pragma: no-cache
Cache-Control: no-cache
{}
RESPONSE
-------
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 19 Jun 2012 16:33:40 GMT
Content-Length: 96
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
我已经不知道我现在阅读了多少篇文章试图解决这个问题。这些说明要么不完整,要么由于某种原因无法解决我的问题。 一些更相关的包括(都没有成功):
- ASP.NET web service erroneously returns XML instead of JSON
- asmx web service returning xml instead of json in .net 4.0
- http://williamsportwebdeveloper.com/cgi/wp/?p=494
- http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/
- http://forums.asp.net/t/1054378.aspx
- http://jqueryplugins.info/2012/02/asp-net-web-service-returning-xml-instead-of-json/
加上其他几篇一般性文章。
最佳答案
终于明白了。
发布的应用代码是正确的。问题出在配置上。正确的 web.config 是:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<handlers>
<add name="ScriptHandlerFactory"
verb="*" path="*.asmx"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>
根据文档,从 .NET 4 向上注册处理程序应该是不必要的,因为它已被移动到 machine.config。无论出于何种原因,这对我不起作用。但是将注册添加到我的应用程序的 web.config 解决了问题。
很多关于这个问题的文章都指示将处理程序添加到 <system.web>
部分。这不起作用,并导致一大堆其他问题。我尝试将处理程序添加到这两个部分,这会产生一组其他迁移错误,这完全误导了我的故障排除。
如果它对其他人有帮助,如果我再次遇到同样的问题,这是我要查看的 list :
- 您是否指定了
type: "POST"
在 ajax 请求中? - 您是否指定了
contentType: "application/json; charset=utf-8"
在 ajax 请求中? - 您是否指定了
dataType: "json"
在 ajax 请求中? - 您的 .asmx 网络服务是否包含
[ScriptService]
属性? - 您的网络方法是否包含
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
属性? (我的代码即使没有这个属性也可以工作,但是很多文章都说它是必需的) - 您是否添加了
ScriptHandlerFactory
到<system.webServer><handlers>
中的 web.config 文件? - 您是否从
<system.web><httpHandlers>
中的 web.config 文件中删除了所有处理程序?
希望这对遇到同样问题的人有所帮助。并感谢海报的建议。
关于c# - asp.net asmx Web 服务返回 xml 而不是 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11088294/