我正在调用Sharepoint URL(GET)并重新获取列信息。我在列表中有列名称,如名字(例如Donald),姓氏(例如Duck)等。 现在我必须得到First Name&姓氏(唐老鸭)并以下拉列表的形式显示。我正在编写下面的代码来获取shareopint响应(XML):
$.ajax({
type: "GET",
beforeSend: function(xhr) { xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password)); },
url: calURL,
crossDomain: true,
//dataType: "xml",
//cache: false,
processData: false,
xhrFields: {
withCredentials: true
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
document.getElementById("demo").innerHTML = "Got stuck";
},
success: function(xml) {
var xmlArr = [];
var html_text = null;
alert($(xml).find('entry').text());
$(xml).find('entry').each(function(){
var title = $(this).find('title').text();
var firstName = $(this).find('First Name').text();
var familyName = $(this).find('Family Name').text();
var fullName = title + firstName + familyName;
});
},
});
答案 0 :(得分:0)
您尚未提供SharePoint响应XML,但您想要的信息最有可能位于行元素attributes
中,而不是text
内容中。此外,此xml中的每个元素都有一个应该引用的命名空间。
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row">
<s:AttributeType name="CustomerID" rs:number="1" rs:keycolumn="true">
<s:datatype dt:type="int" dt:maxLength="4"/>
</s:AttributeType>
<s:AttributeType name="Title" rs:number="2">
<s:datatype dt:type="string" dt:maxLength="5" />
</s:AttributeType>
<s:AttributeType name="FirstName" rs:number="3">
<s:datatype dt:type="string" dt:maxLength="15" />
</s:AttributeType>
<s:AttributeType name="LastName" rs:number="4">
<s:datatype dt:type="string" dt:maxLength="15"/>
</s:AttributeType>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row CustomerID="123" Title="Mr." FirstName="Donald" LastName="Duck"/>
<z:row CustomerID="456" Title="Mrs." FirstName="Minney" LastName="Mouse"/>
<z:row CustomerID="789" Title="Mr." FirstName="Stuart" LastName="Little"/>
</rs:data>
</xml>
如果您的响应XML与上述内容类似,请将ajax成功处理程序更改为
success: function(xml) {
$("rs\\:data", xml).find("z\\:row").each(function() {
var id = $(this).attr('CustomerID');
var title = $(this).attr('Title');
var firstName = $(this).attr('FirstName');
var lastName = $(this).attr('LastName');
var fullName = title + firstName + lastName;
// Add this as an option to <select id="customers"> dropdown list
var option = '<option value="' + id + '">' + fullName + '</option>';
$("#customers").append(option);
});
}