我试图在Sharepoint 2013上使用其余的api创建一个简单的列表项。 我的代码:
$.ajax({
url: siteUrl + "/_api/web/lists/getByTitle('internal_Listname')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify({
'__metadata': {
'type': 'SP.Data.internal_ListnameListItem',
},
'K1F1': k1f1Result,
}),
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function (data) {
console.log("done");
},
error: function (err) {
console.log(JSON.stringify(err));
}
});
当尝试发送数据时,我得到403"禁止"错误。
"error":{
"code":"-2130575251, Microsoft.SharePoint.SPException",
"message":{
"lang":"en-US",
"value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."
}
}
答案 0 :(得分:18)
由于页面上的表单摘要已过期,因此很可能会发生此错误。
在这种情况下,您可以通过向POST
端点发出/_api/contextinfo
请求来获取新的表单摘要值。
示例
function getFormDigest(webUrl) {
return $.ajax({
url: webUrl + "/_api/contextinfo",
method: "POST",
headers: { "Accept": "application/json; odata=verbose" }
});
}
function createListItem(webUrl, listName, itemProperties) {
return getFormDigest(webUrl).then(function (data) {
return $.ajax({
url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
type: "POST",
processData: false,
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue
}
});
});
}
用法
//Create a Task item
var taskProperties = {
'__metadata': { 'type': 'SP.Data.WorkflowTasksItem' },
'Title': 'Order approval'
};
createListItem(_spPageContextInfo.webAbsoluteUrl, 'Workflow Tasks', taskProperties)
.done(function (data) {
console.log('Task has been created successfully');
})
.fail(function (error) {
console.log(JSON.stringify(error));
});
答案 1 :(得分:1)
几天前找到解决方案: 我忘了将请求摘要表单添加到正文中。它应该具有以下结构;
<form runat="server">
<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
</form>
答案 2 :(得分:0)
我对同样问题的解决方案:
<form id="form1" runat="server"> <!-- this make SP 2013 take it legit -->
<div class="style1"> <!-- dont know what, but SP need it -->
---your page usually a divs---
</div>
</form>