C# 虽然返回格式为json,但返回xml的web服务
这是web服务C# 虽然返回格式为json,但返回xml的web服务,c#,jquery,ajax,web-services,C#,Jquery,Ajax,Web Services,这是web服务 public class HelloWorld : System.Web.Services.WebService { [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string Hello() { return "Hello World"; } } 下面是javascript ajax代码 $(document).read
public class HelloWorld : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Hello()
{
return "Hello World";
}
}
下面是javascript ajax代码
$(document).ready(function () {
$.ajax({
url: "Service/HelloWorld.asmx/Hello",
dataType: 'json',
type: 'POST',
cache: false,
crossDomain: true,
timeout: 15000,
success: function (rtndata) {
alert('Success : :' + rtndata);
},
error: function (xhr, errorType, exception) {
alert("Excep:: " + exception + "Status:: " + xhr.statusText);
}
});
});
获取错误
Excep:: Invalid JSON: <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>Status:: OK
Excep::无效的JSON:
你好,WorldStatus::好的
尝试使用
[ScriptMethod(UseHttpPost=true, ResponseFormat=ResponseFormat.Json)]
还可以从您的服务返回一个对象,以便客户端可以更可预测地使用它
return new Result() { Value = "some string" };
确保您在web.config的Handler部分下设置了ScriptHandlerFactory
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
</httpHandlers>
编辑
我建议您浏览以下Dave Ward的博客文章,讨论安装和配置错误: