【问题标题】:ASMX Web service not returning to jQueryASMX Web 服务未返回到 jQuery
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您的函数 GetAccountTypes() 等待 json 格式...您确定该服务以 json 格式返回字典吗?我认为...没有。

    Web 服务是基于 SOAP 的 Web 服务,您可以考虑将您的 Web 服务迁移到 WCF,您可以在其中对所需的输出格式进行大量控制。

    查看此材料

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多