【发布时间】:2012-04-20 13:21:20
【问题描述】:
我已经阅读了这里的帖子并尽我所知,但仍然无法让我的网络服务返回 json。
网络服务,.Net 4.0
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class JsonWS : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string Sum()
{
string x = "1", y = "2";
return x + y;
}
}
这是我的 jquery 调用。
<script>
$(function () {
$('#btn_test').click(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost/jsontest/JsonWS.asmx/Sum",
dataType: "json",
success: function (json) {
alert(json.d);
},
error: function () {
alert("Hit error fn!");
}
});
});
});
此错误 b/c web 服务正在返回...
<string xmlns="http://tempuri.org/">12</string>
谢谢。
【问题讨论】:
-
GET请求是否成功?
-
获取请求也不起作用。我读过的其他帖子提到它只适用于帖子。
-
你试过Accept: application/json HTTP header吗?
-
刚刚尝试添加 Accept 标头。没有帮助。
-
加法的有趣定义...
标签: jquery .net json web-services