在使用REST向SharePoint列表添加和删除项目时,出现403个禁止请求和400个坏请求的错误。

问题描述 投票:0回答:1

我是SharePoint开发的新手。我想使用SharePoint在线开发简单的SharePoint应用。我的网站集合中有一个名为 "产品 "的列表。在我的应用程序中,我写了以下代码来添加和删除该列表中的项目。

 function addProduct(product) {
 var executor;
 executor = new SP.RequestExecutor(appwebUrl);
 var url = appwebUrl +"/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Products')/items/?@target='" + hostwebUrl+"'";
 executor.executeAsync({
    url: url,
    method: "POST",
    body: JSON.stringify({__metadata: { type: 'SP.Data.ProductsListItem' },
        Title: product.ProductName(),
        ProductId: product.ProductId(),
        ProductName: product.ProductName(),
        Price:product.Price()
    }),
    headers: {
        "Accept": "application/json; odata=verbose",
        "content-type": "application/json;odata=verbose",
        },
    success: successProductAddHandler,
    error: errorProductAddHandler
});
}


function successProductAddHandler(data) {alert('added successfully') }
function errorProductAddHandler(data, errorCode, errorMessage) { alert('cannot perform action') }


function deleteProduct(product) {
var executor;
executor = new SP.RequestExecutor(appwebUrl);
var url=appwebUrl+"/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Products')/items('" + product.ID() + "')/?@target='" + hostwebUrl + "'";
executor.executeAsync({
    url: url,
    method: "POST",
    headers: {

        "IF-MATCH": "*",
        "X-HTTP-Method": "DELETE"
    },
    success: successProductAddHandler,
    error: errorProductAddHandler
});`

我越来越 403 错误代码,当我调用 addProduct以及 400 错误代码,当我调用 deleteProduct.我能够得到列表项并显示。

我尝试添加 X-RequestDigest": $("#__REQUESTDIGEST").val() 却不灵了

如果我包括 "Accept": "application/json; odata=verbose" 的请求头中。deleteProduct()而当我打电话 deleteProduct,两个请求将被发送到服务器

  1. /sites/productsdev/productsapp/_api/contextinfo (获取摘要值)
  2. /sites/ProductsDev/ProductsApp/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Products')/items(itemid)/?@target='mysitecollectionurl' (使用上面调用的 X-RequestDigest)
rest sharepoint sharepoint-2013
1个回答
3
投票

当您在SharePoint 2013中使用REST API进行任何POST操作时,您必须在头中传递以下代码段。

"X-RequestDigest":$("#__REQUESTDIGEST").val()

蛋蛋

头条。{ "Accept".X-RequestDigest:$("#__REQUESTDIGEST".val() "applicationjson;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() }。

© www.soinside.com 2019 - 2024. All rights reserved.