[英]Delete item from SharePoint List using JavaScript and REST
I have some JavaScript (physical file located in /SiteAssets library) that should delete an item in a SharePoint list. 我有一些JavaScript(位于/ SiteAssets库中的物理文件),应该删除SharePoint列表中的项目。
We have existing JavaScript code that retrieves data from the list - it looks like this: 我们现有的JavaScript代码可从列表中检索数据-看起来像这样:
(notice that since the JavaScript runs on a PDP in context of the current user, we don't need a specific access token for the request) (请注意,由于JavaScript在当前用户的上下文中在PDP上运行,因此我们不需要该请求的特定访问令牌)
var data = $.ajax({
url: projSiteUrl + "/_api/lists/getbytitle('<listname>')/items,
type: "GET",
dataType: "json",
async: false,
headers: {
Accept: "application/json;odata=verbose"
}
});
So I thought that I could write similar code to delete an item from the list again. 因此,我认为我可以编写类似的代码来再次从列表中删除一个项目。 I read on https://msdn.microsoft.com/en-us/library/office/jj164022.aspx#HTTPOps that the REST endpoint of SharePoint supports the "normal" REST verbs, so I wrote this, using the
DELETE
HTTP verb. 我在https://msdn.microsoft.com/zh-cn/library/office/jj164022.aspx#HTTPOps上阅读到,SharePoint的REST端点支持“正常” REST动词,因此我使用
DELETE
HTTP动词编写了此代码。
var restUrl = spSiteUrl + '/_api/web/lists/GetByTitle(\'' + listTitle + '\')/items(' + itemId + ')';
jQuery.ajax({
url: restUrl,
type: "DELETE",
headers: {
Accept: "application/json;odata=verbose"
}
})
But I am getting a 403 (FORBIDDEN)
when requesting. 但是请求时我得到了
403 (FORBIDDEN)
。
I guess the question is: Am I wrong in assuming that the DELETE
verb is supported? 我猜问题是:假设支持
DELETE
动词是否对吗?
Thanks :-) 谢谢 :-)
Ok, so apparently I do need the digest when doing modifications - but not for simple data retrieval. 好的,因此显然在进行修改时确实需要摘要-但对于简单的数据检索来说并不需要。
If I change my code to 如果我将代码更改为
jQuery.ajax({
url: restUrl,
type: "DELETE",
headers: {
Accept: "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*"
}
}).
... it works with a simple AJAX request using the REST HTTP verb DELETE
:-) ...它与使用REST HTTP动词
DELETE
的简单AJAX请求一起使用:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.