大佬教程收集整理的这篇文章主要介绍了asp.net asmx Web服务返回xml而不是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>
部分。这是行不通的,并且会导致其他问题。我尝试将处理程序添加到这两个部分中,这会产生一系列其他迁移错误,这些错误完全导致了我的故障排除方向。
万一它对其他人有帮助,如果我再次遇到同样的问题,请查看以下清单:
type: "POST"
在AJAX请求中指定了?ContentType: "application/Json; charset=utf-8"
在AJAX请求中指定了?dataType: "Json"
在AJAX请求中指定了?[Scriptservice]
属性?[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
属性?(即使没有此属性,我的代码也可以工作,但是很多文章说这是必需的)ScriptHandlerFactory
到web.config文件中<system.webServer><handlers>
?<system.web><httpHandlers>
?希望这对遇到同样问题的人有所帮助。并感谢海报的建议。
为什么这个简单的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形式返回。如何获得Web服务以返回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/20@R_262_9715@1 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 asmx Web服务返回xml而不是json全部内容,希望文章能够帮你解决asp.net asmx Web服务返回xml而不是json所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。