ASMX Web服务未返回jQuery
解决方法:不能从AJAX WEB服务返回字典,请使用列表 我正在尝试从jQuery调用ASP.NET Web服务 Web服务运行,但结果不会返回到javascript。知道为什么吗?我想我可能在web.config或..中遗漏了什么 jQuery:ASMX Web服务未返回jQuery,jquery,asp.net,ajax,web-services,asmx,Jquery,Asp.net,Ajax,Web Services,Asmx,解决方法:不能从AJAX WEB服务返回字典,请使用列表 我正在尝试从jQuery调用ASP.NET Web服务 Web服务运行,但结果不会返回到javascript。知道为什么吗?我想我可能在web.config或..中遗漏了什么 jQuery: function GetAccountTypes() { var ddl = $("#ListBoxType"); clearSelect(ddl.attr("id")); $.ajax({
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;
}
}
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类WebService:System.Web.Services.WebService{
公共Web服务()
{
}
[网络方法]
公共字典GetAccountTypes(){
字典类型=新ATDB().GetAccountTypes();
返回类型;
}
}
web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
在js函数和web服务方法中设置断点表明该方法运行,但从未达到“成功”或“失败”。您的函数GetAccountTypes()等待json格式。。。您确定服务以json格式返回字典吗??我想。。。没有
Web服务是基于SOAP的Web服务,您可以考虑将Web服务迁移到WCF,在其中,您将对所需的输出格式有很大的控制。 看看这些材料
我发现不能从Web服务返回字典,因为它实现了IDoctionary,并且不可序列化。解决方案:返回一个简单自定义对象的列表。Ajax程序员最好的朋友:Fiddler。使用它来确定web服务器的实际响应是什么。这会告诉你很多可能的错误。函数GetAccountTypes()等待json格式。。。您确定服务以json格式返回字典吗??