[英]Sharepoint Rest Api calls`
我正在建立一個簡單的網站,我必須將我的一些數據保存到sharepoint列表中。 我只使用html css js。 我需要對Sharepoint API進行REST調用以在SP上發布數據。 我試圖從我的列表中獲取數據(在SP中)如下:
$(document).ready(function() {
processListItems(hostweburl, 'ListName');
});
function processListItems(url, listname) {
$.ajax({
url: url + "/_api/web/lists/getbytitle('" + listname + "')",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
});
}
它返回此響應:
"{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}"
我只引用了兩個腳本:
<script src="js/jquery-3.1.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>
它說我未經授權訪問。 雖然我對我的列表擁有管理員權限。 請告訴我如何解決這個問題。 我是否需要任何授權標頭來調用Rest Api的ajax? 我是Sharepoint的新手。 請幫忙!
我認為默認情況下在SPOnline中禁用自定義腳本。 您可能必須啟用它。 這些鏈接有助於:
使用全局管理員帳戶訪問拒絕Office 365 / SharePoint聯機
http://vamsi-sharepointhandbook.blogspot.com/2015/07/turn-scripting-capabilities-on-or-off.html
當您嘗試從列表中提取項目時,請指定最多為項目的URL。 請參閱以下行語法 -
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('"+listName+"')/items",
// Rest of the code
});
是的,您需要一個授權標頭來滿足您的要求。 所以你的標題應該是這樣的:
headers: {
"Accept": "application/json; odata=verbose",
"Authorization": 'Bearer '+ **access_token**
}
access_token:您需要獲取訪問令牌才能發出請求。 您可以使用Microsoft提供的adal庫 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.