我已经阅读了这里的帖子并尽我所知,但仍然无法让我的网络服务返回 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>
谢谢。