2015-09-24 29 views
0

我實際上在使用SharePoint 2013應用程序(SharePoint託管),並且想要獲取SharePoint列表的項目標題。因此,我使用ajax的HTTP-GET函數,但現在評估返回的XML對象時出現問題。Sharepoint 2013:評估ajax GET請求(XML)

這裏是我的ajax請求代碼:

var requestURL = appweburl + "/_api/lists/getbytitle('Fragenkatalog')/items?$select=Title"; 

$.ajax({ 
     type: "GET", 
     url: requestURL, 
     dataType: "xml", 
     success: function (data) { 
      showResult(data); 
     }, 
     error: function (error) { 
      console.log("ERROR REST"); 
     } 
    }); 

得到了我在這裏的第一個問題,在開始的時候我試圖讓JSON從GET請求的結果,所以我改變了數據類型爲JSON。在我做完這些之後,我總是在錯誤功能中運行。我忘了在那裏添加什麼或者爲什麼它沒有工作?

的XML文檔,我得到:

<?xml version="1.0" encoding="UTF-8" ?> 
<feed xml:base=http://apps-385225078ec0a6.sp.xyz/_api/ xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"> 
     <id>fd01da84-e9ec-4872-842d-d3d29af08119</id> 
     <updated>2015-09-24T09:57:46Z</updated> 
     <entry m:etag="&quot;1&quot;"> 
      <id>Web/Lists(guid'86b1cc4b-a52e-4d56-a1be-24a2bccd25c5')/Items(1)</id> 
      <category term="SP.Data.FragenkatalogListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link rel="edit" href="Web/Lists(guid'86b1cc4b-a52e-4d56-a1be-24a2bccd25c5')/Items(1)" /> 
      <updated>2015-09-24T09:57:46Z</updated> 
      <author /> 
      <content type="application/xml"> 
       <m:properties> 
        <d:Title>Element 1</d:Title> 
       </m:properties> 
      </content> 
     </entry> 
     <entry m:etag="&quot;1&quot;"> 
      <id>Web/Lists(guid'86b1cc4b-a52e-4d56-a1be-24a2bccd25c5')/Items(2)</id> 
      <category term="SP.Data.FragenkatalogListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link rel="edit" href="Web/Lists(guid'86b1cc4b-a52e-4d56-a1be-24a2bccd25c5')/Items(2)" /> 
      <updated>2015-09-24T09:57:46Z</updated> 
      <author /> 
      <content type="application/xml"> 
       <m:properties> 
        <d:Title>Element 2</d:Title> 
       </m:properties> 
      </content> 
     </entry> 

    </feed> 

這裏是我的結果功能

function showResult(data) { 

    $(data).find('entry').each(function() { 
     $(this).find("content").each(function() { 
      $(this).find("m:properties").each(function() { 
       var Titel = $(this).find("d:Title").text(); 
       console.log(Titel); 
      }); 
     }); 
    }); 
} 

現在,我想每個項目的冠軍在我的名單。 但是.find在搜索「m:properties」標籤時總是失敗,這就是爲什麼我無法獲得我正在尋找的信息。

也許你們有人可以幫助我。

感謝&親切的問候

塞巴斯蒂安

回答

0

當調用的SharePoint REST API,你可以通過Accept頭請求返回控制數據的格式。下面的例子演示瞭如何如圖所示圖片下面

enter image description here

實施例1

var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('" + listTitle + "')/items?$select=Title"; 

function getJson(requestUrl) 
{ 
    return $.ajax({ 
     type: "GET", 
     url: requestUrl, 
     dataType: "json", 
     headers: { "Accept" : "application/json;odata=verbose" } 
    }); 
} 


getJson(requestUrl) 
.done(function(data){ 
    //print results 
    $.each(data.d.results,function(i,item){ 
     console.log(item.Title); 
    }); 
}); 

實施例2

當省略Accept頭返回JSON格式數據, SharePoint REST以下圖所示的JSON格式返回數據(minimalmetadata option

enter image description here

var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('" + listTitle + "')/items?$select=Title"; 

$.getJSON(requestUrl,function(data){ 
    //print results 
    $.each(data.value,function(i,item){ 
     console.log(item.Title); 
    }); 
}); 
+1

感謝隊友!它很棒! – Sebastian