ASMX Web服务没有返回jQuery

时间:2013-03-12 00:33:32

标签: jquery asp.net ajax web-services asmx

已解决:您无法从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服务方法中设置断点表明该方法运行但它永远不会达到“成功”或“失败”。

2 个答案:

答案 0 :(得分:2)

好吧,我发现你不能从Web服务返回一个Dictionary,因为它实现了IDoctionary并且不可序列化。解决方案:返回一个简单自定义对象的列表。

答案 1 :(得分:0)

你的函数GetAccountTypes()等待json格式...你确定该服务以json格式返回字典吗?我想......不。

Web服务是基于SOAP的Web服务,您可以考虑将Web服务迁移到WCF,在WCF中您可以很好地控制所需的输出格式。

查看此资料

http://blog.clauskonrad.net/2010/11/how-to-expose-json-endpoint-from-wcf.html