为什么这个简单的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);
}
});
Run Code Online (Sandbox Code Playgroud)
和服务:
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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
web.config中:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
并回应:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
Run Code Online (Sandbox Code Playgroud)
无论我做什么,响应总是以XML形式返回.如何让Web服务返回Json?
编辑:
这是Fiddler HTTP跟踪: …