2017-05-05 135 views
0

我試圖用的Sharepoint REST API(我們剛剛開始與SharePoint API,以便definitly沒有專家)來創建一個的ListItem
按照微軟tutorial後應該是這樣的:
enter image description here的SharePoint REST API返回錯誤請求(400)

我實現下面的代碼

public addItemToList_Test(): void { 

var listTitle: string = "DemoHomeWork"; 
var listItemType: string = "SP.Data." + listTitle + "ListItem"; 
var listItemTitle: string = "TestItem"; 
var postBody = { '__metadata': { 'type': listItemType }, 'Title': 'TestItem' }; 

var $: jQuery = require("jquery"); 
var call = $.ajax({ 
    url: listsUrl + "/GetByTitle('" + listTitle + "')/items", 
    method: "POST", 
    body: postBody, 
    headers: { 
    "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
    "accept": "application/json;odata=verbose", 
    "content-type": "application/json;odata=verbose", 
    length: JSON.stringify(postBody).length 
    } 
}); 

return call; 
} 

然而,這樣下去回我錯誤請求(400)並弄清楚爲什麼會發生這種情況是非常棘手的。有沒有人能告訴我這個請求有什麼問題?

回答

0

您如何驗證您的請求?

如文檔中給出的,您需要將access_token與您的請求一起傳遞給API以使其工作。正如你在頭上,我沒有看到access_token被傳遞。

您需要先獲得access_token,然後將其傳遞給API以使其工作。

"'__metadata': { 'type': listItemType }" **listItemType** you are creating it manually but it can be different. Can you check getting it manually (`https://site_url/_api/web/lists/getbytitle(listtitle)`) 

此外,檢查是否ListItemEntityTypeFullName這個名單是一樣的,你已經創建了listItemType

相關問題