【发布时间】:2013-03-12 00:33:32
【问题描述】:
已解决:您无法从 AJAX 网络服务返回字典,请使用列表!
我正在尝试从 jQuery 调用 ASP.NET Web 服务。
Web 服务运行,但结果未返回到 javascript。知道为什么吗?我想我可能在 web.config 或.. 中遗漏了一些东西?
jQuery:
function GetAccountTypes() {
var ddl = $("#ListBoxType");
clearSelect(ddl.attr("id"));
$.ajax({
type: "POST",
url: "WebService.asmx/GetAccountTypes",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
var accTypes = response.d;
var options = ddl.attr("options");
$.each(accTypes, function (index, accType) {
options[options.length] = new Option(accType.Value, accType.Key);
});
},
failure: function (msg) {
alert(msg);
}
});
}
网络服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService {
public WebService()
{
}
[WebMethod]
public Dictionary<int, string> GetAccountTypes() {
Dictionary<int, string> types = new ATDB().GetAccountTypes();
return types;
}
}
web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
在js函数和web服务方法中设置断点,表示方法运行但从未达到'成功'或'失败'。
【问题讨论】:
-
Ajax 程序员最好的朋友:Fiddler。使用它来确定 Web 服务器的实际响应是什么。这会告诉你很多关于什么是错误的。 fiddler2.com/fiddler2
-
您的函数 GetAccountTypes() 等待 json 格式...您确定服务返回 json 格式的字典吗??
标签: jquery asp.net ajax web-services asmx