我是 SharePoint 开发的新手。我正在尝试使用 SharePoint Online 开发简单的 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
,两个请求将发送到服务器
/sites/productsdev/productsapp/_api/contextinfo
(获取摘要值)/sites/ProductsDev/ProductsApp/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Products')/items(itemid)/?@target='mysitecollectionurl'
(使用上述调用返回的摘要值 forX-RequestDigest
)