簡體   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