为什么这个简单的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/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>
我已经记不清我现在已经读了多少篇试图解决这个问题的文章。这些说明要么不完整,要么由于某种原因没有解决我的问题。一些更相关的方法包括(都没有成功):
再加上其他几篇一般性的文章。
发布于 2012-06-20 21:18:17
终于想明白了。
应用程序代码如帖子所示是正确的。问题出在配置上。正确的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"
?contentType: "application/json; charset=utf-8"
?dataType: "json"
您的.asmx web服务是否包含<代码>D13您的web方法是否包含<代码>D16属性?(我的代码即使没有这个属性也能正常工作,但是很多文章都说,正是因为在<system.webServer><handlers>
ScriptHandlerFactory
到web.config文件中,才从<system.web><httpHandlers>
中的web.config文件中删除了所有处理程序
希望这篇文章能帮助任何有同样问题的人。并感谢海报上的建议。
发布于 2012-07-18 01:16:43
上面的解决方案没有成功,下面是我如何解决它的。
将这一行放入您的the服务中,而不是返回类型,只需在响应上下文中写入字符串
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(serial.Serialize(city));
发布于 2013-09-08 21:46:06
如果您想继续使用Framework3.5,则需要对代码进行如下更改。
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
}
[WebMethod]
public void HelloWorld() // It's IMP to keep return type void.
{
string strResult = "Hello World";
object objResultD = new { d = strResult }; // To make result similarly like ASP.Net Web Service in JSON form. You can skip if it's not needed in this form.
System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
string strResponse = ser.Serialize(objResultD);
string strCallback = Context.Request.QueryString["callback"]; // Get callback method name. e.g. jQuery17019982320107502116_1378635607531
strResponse = strCallback + "(" + strResponse + ")"; // e.g. jQuery17019982320107502116_1378635607531(....)
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.AddHeader("content-length", strResponse.Length.ToString());
Context.Response.Flush();
Context.Response.Write(strResponse);
}
}
https://stackoverflow.com/questions/11088294
复制相似问题