繁体   English   中英

如何将参数传递给asp.net Web服务并返回xml?

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

目前,我有一个按钮,使用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数据,但可以帮助您进行发送。

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

data: { "CustomerName": customer },

如果客户变量属于复杂类型,请使用

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM