【问题标题】:How to pass on a parameter to asp.net web service and return xml?如何将参数传递给 asp.net Web 服务并返回 xml?
【发布时间】:2011-04-20 07:39:01
【问题描述】:

目前我有一个按钮,它使用 jQuery/AJAX 从 SharePoint 列表中搜索所有客户,并且我的 Web 服务返回一个 XML 字符串。然后,我使用 XML 中的数据填充下拉列表。

我知道想为搜索功能传递一个参数(客户名称),我可以从 SharePoint 列表中返回我想要的内容,但我的 AJAX 调用返回错误 (parseerror)。

获取所有客户(可行):

$.ajax({
    type: "GET",
    url: "SynchroniseCustomers.asmx/GetAllCustomers",
    dataType: "text/xml",

error: function (xhr, status) {
    hideLoading();
},
beforeSend: function () {
    showLoading("customers");
},
success: function (xml) {
    hideLoading();
    populatecustomerDropdownList($(xml).text());
}

});

我不知道该怎么做,但我试过了

var customer = CustomerName;

$.ajax({
    type: "GET",
    data: { CustomerName: JSON.stringify(customer) },
    url: "SynchroniseCustomers.asmx/GetCustomerByName",
    dataType: "json",

error: function (xhr, status) {
    hideLoading();
    alert(xhr + " " + status);
},
beforeSend: function () {
    showLoading("Customers");
},
success: function (xml) {
    hideLoading();
    populateCustomerDropdownList($(xml).text());
}
});

有人可以为我指出如何执行此操作的正确方向吗?

提前致谢。

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:

    您将返回数据类型指定为 JSON,它应该是 XML:

    dataType: "xml"
    

    这看起来也不对:

    populatecustomerDropdownList($(xml).text());
    

    当您执行$(xml) 时,您可以像访问 HTML 一样访问您的结构,例如,如果结构是:

    <?xml version="1.0" encoding="utf-8" ?>
    <RecentTutorials>
      <Tutorial author="The Reddest">
        <Title>Silverlight and the Netflix API</Title>
        <Categories>
          <Category>Tutorials</Category>
          <Category>Silverlight 2.0</Category>
          <Category>Silverlight</Category>
          <Category>C#</Category>
          <Category>XAML</Category>
        </Categories>
        <Date>1/13/2009</Date>
      </Tutorial>
    

    jQuery:

      success: function(xml) {
         $(xml).find("Tutorial").each(function()
         {
            $("#output").append($(this).attr("author") + "<br />");
         });
      }
    

    【讨论】:

    • 谢谢 Gary,我将 xml 传递给一个名为 populatecustomerDropdownList 的函数
    【解决方案2】:

    我不知道如何从 Web 服务返回 XML 数据,但可以在发送位方面为您提供帮助。

    如果客户变量只是一个简单的字符串,使用

    data: { "CustomerName": customer },
    

    如果客户变量是复杂类型,则使用

    data: { "CustomerName": JSON.stringify(customer) },
    

    有关传递复杂类型的更多信息,请阅读article by Dave Ward

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-12
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      相关资源
      最近更新 更多