2011-04-20 110 views
0

目前我有一个按钮,使用jQuery/AJAX从SharePoint列表中搜索所有客户,并且我的Web服务返回一个XML字符串。然后我使用XML中的数据填充下拉列表。如何将参数传递给asp.net web服务并返回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的客户=客户名称;

$.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()); 
} 
}); 

有人可以请指出我在正确的方向如何执行此?

在此先感谢。

回答

0

你specificing您返回的数据类型为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 />"); 
    }); 
    } 
+0

谢谢Gary,我将xml传递给一个名为populatecustomerDropdownList的函数 – Nick 2011-04-20 09:32:42

0

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

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

data: { "CustomerName": customer }, 

如果客户变量是复合型的,使用

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

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

相关问题