如何传递参数到asp.net web服务并返回xml

How to pass on a parameter to asp.net web service and return xml?

本文关键字:服务 返回 xml web net 何传递 参数 asp      更新时间:2023-09-26

目前我有一个按钮,用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());
}
});

谁能告诉我该怎么做吗?

如果指定返回数据类型为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 />");
     });
  }

我不知道从web服务返回XML数据,但可以帮助您发送位。

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

data: { "CustomerName": customer },

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

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

有关传递复杂类型的更多信息,请阅读Dave Ward撰写的文章。