我想对SharePoint列表进行更新。我看到标题必须包含"X-HTTP-Method": "MERGE"
,类型应为POST
。我不知道"IF-MATCH": "*"
是否需要有etag。当我删除某个项目时,"IF-MATCH": "*"
不需要有etag。这是我的代码:
function UpdateBooking(ajaxData) {
var url = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/lists/getbytitle('Bokningar')/items`;
//requestHeaders
var requestHeaders = {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-HTTP-Method": "MERGE",
"IF-MATCH": "*",
"X-RequestDigest": jQuery('#__REQUESTDIGEST').val()
}
//Data
var data = {
__metadata: {
"type": "SP.Data.BokningarListItem"
},
Title: ajaxData.Title,
EventDate: ajaxData.EventDate,
EndDate: ajaxData.EndDate,
Description: ajaxData.Description,
IsBlock: ajaxData.IsBlock,
Milj_x00f6_: ajaxData.Milj_x00f6_,
Program: ajaxData.Program
};
//requestBod
var requestBody = JSON.stringify(data);
//Post
var post = jQuery.ajax({
url: url,
type: "POST",
headers: requestHeaders,
data: requestBody
});
//Done
post.done(function(data, textStatus, jqXHR) {});
//Fail
post.fail(function(jqXHR, textStatus, errorThrown) {});
}
这是我得到的错误:
{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The type SP.ListItemEntityCollection does not support HTTP PATCH method."}}}
任何想法我做错了什么?
答案 0 :(得分:2)
解决了它。我没有在URL中提供id。
var url = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/lists/getbytitle('Bokningar')/items('${id}')`;