我有一个 ASP.Net 网页尝试使用 jquery .axax 方法从 asmx web 服务检索数据。当dataType =“text”时,ajax方法正确调用成功方法,但是当使用“json”的dataType时,我无法让它返回。谁能看到我缺少什么吗?我在网上获取“json”示例http://weblogs.asp.net/jaredroberts/archive/2009/08/28/great-article-on-cascading-dropdown-list-and-jquery.aspx http://weblogs.asp.net/jaredroberts/archive/2009/08/28/great-article-on-cascading-dropdown-list-and-jquery.aspx
Client:
function getText() {
alert("getText");
$.ajax({
type: "POST",
url: "test.asmx/HelloWorld",
dataType: "text",
success: function(response) { alert("text"); }
});
}
function getJson() {
alert("getJson");
$.ajax({ type: "POST",
url: "test.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) { alert("json"); }
});
}
服务器端 Web 服务调用:
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
最后,我的问题的根源是类装饰上缺少 [ScriptService] 属性。我将类声明更改为:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SearchFilters : System.Web.Services.WebService {
[WebMethod]
public string HelloWorld() {
return "";
}
}
使用 Fiddler 我发现返回了以下错误消息:
仅类上具有 [ScriptService] 属性的 Web 服务
可以从脚本调用定义
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)