Javascript Sharepoint 2013 通过 REST API:尝试创建项目时出现错误 403 Forbidden

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32435432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 08:02:45  来源:igfitidea点击:

Sharepoint 2013 via REST API: Error 403 Forbidden when trying to create item

javascriptajaxjsonrestsharepoint

提问by sandrooco

I'm trying to create a simple list item with the rest api on Sharepoint 2013. My code:

我正在尝试使用 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));
    }
});

When trying to send the data I get the 403 "Forbidden" error.

尝试发送数据时,我收到 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."
    }
}
  • I have full admin privileges on this site and the list.
  • 我在此站点和列表上拥有完整的管理员权限。

采纳答案by sandrooco

Found the solution a few days ago: I forgot to add the request digest form to the body. It should have the following structure;

几天前找到了解决方案:我忘记将请求摘要表单添加到正文中。它应该具有以下结构;

<form runat="server">
   <SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
</form>

回答by Vadim Gremyachev

Most likely this error occurs since form digest has been expiredon the page.

由于页面上的表单摘要已过期,因此很可能会发生此错误。

In that case you could acquire a new form digest value by making a POSTrequest to /_api/contextinfoendpoint.

在这种情况下,您可以通过POST/_api/contextinfo端点发出请求来获取新的表单摘要值。

Example

例子

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
            }
        });
    });
}

Usage

用法

//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));
});

回答by leonardo rey

My solution to the same problem:

我对同样问题的解决方案:

<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>