已解决:您无法从 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;
}
}
网络配置:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
在 js 函数和 web 服务方法中设置断点表明该方法运行但它从未达到“成功”或“失败”。