2017-05-05 102 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

相关问题