SharePoint / Angular无法删除项目 - 错误412

时间:2015-03-16 15:07:30

标签: angularjs rest sharepoint sharepoint-2013

更新:我已按如下方式更改了服务和控制器,现在收到错误412(HTTP / 1.1 412 Precondition Failed)。这是我更新的代码:

服务:

appServices.factory('appType', ['$resource', function ($resource) {
    return $resource("/_api/web/lists/getbytitle('Todo Types List')/Items(:Id)", { Id: "@Id" },
    {
        'query': { method: "GET", isArray: false, headers: { 'Accept': 'application/json;odata=nometadata' } },
        'update': { method: 'PATCH', headers: { 'Accept': 'application/json;odata=nometadata' } },
        'save': { method: 'POST', headers: { 'Accept': 'application/json;odata=nometadata', 'content-type': 'application/json;odata=nometadata', 'X-RequestDigest': $("#__REQUESTDIGEST").val() } },
        'delete': { method: 'DELETE', headers: { 'Accept': 'application/json;odata=nometadata', 'content-type': 'application/json;odata=nometadata', 'X-RequestDigest': $("#__REQUESTDIGEST").val() }, 'IF-MATCH': "*" },
    }
    );
}]);

Controller的相关部分:

$scope.removeType = function (type) {

appType.delete({ Id: type.Id });
console.log("Deleted" + type.ID);
}
}]);

原帖:

我正在尝试删除构建于SharePoint 2013之上的Angular App中的项目/行,但是当我尝试删除时,它似乎要处理,但不会从SharePoint列表中删除任何内容。使用IE中的网络选项卡,我收到400错误。

这是我的控制器:

appControllers.controller('appSettingsCtrl', ['$scope', 'appTypes', function ($scope, appTypes) {

// Retrieve Types
$scope.types = [];


appTypes.query({}, function (data) {
    $scope.types = data.value;
});


// Create Types
var typeEntry = new appTypes;

$scope.addType = function () {
    console.log("Clicked");
    typeEntry.Title = $scope.itemtype;
    typeEntry.$save();
}

// Delete types
$scope.removeType = function (type) {

appTypes.delete({}, { Id: type.ID });
console.log("Deleted" + type.ID);
}
}]);

这是我的HTML:

    <table class="table table-striped">
        <tr>
            <th>Type</th>
            <th>
                Action
            </th>
        </tr>

        <tr ng-repeat="type in types" id="type{{type.Id}}">

            <td>
                {{type.Title}}
            </td>
            <td>
                <button type="button" class="btn btn-danger" data-ng-click="removeType(type)">Delete</button>
            </td>
        </tr>
    </table>

这是我的服务:

   appServices.factory('appTypes', ['$resource', function ($resource) {
    return $resource("/_api/web/lists/getbytitle('Todo Types List')/Items", {Id: "@Id" },
    {
        'query': { method: "GET", isArray: false, headers: { 'Accept': 'application/json;odata=nometadata' } },
        'update': { method: 'PATCH', headers: { 'Accept': 'application/json;odata=nometadata' } },
        'save': { method: 'POST', headers: { 'Accept': 'application/json;odata=nometadata', 'content-type': 'application/json;odata=nometadata', 'X-RequestDigest': $("#__REQUESTDIGEST").val() } },
        'delete': { method: 'DELETE', headers: { 'Accept': 'application/json;odata=nometadata', 'content-type': 'application/json;odata=nometadata', 'X-RequestDigest': $("#__REQUESTDIGEST").val(), 'IF-MATCH': '*'  } },
    }
    );
}]);

以下是一些示例JSON:

{
"value": [
    {
        "FileSystemObjectType": 0,
        "Id": 5,
        "ID": 5,
        "ContentTypeId": "0x01004CE051F4BDBACB43BB22C234F8F497FE",
        "Title": "Type 1",
        "Modified": "2015-03-13T18:35:09Z",
        "Created": "2015-03-13T18:35:09Z",
        "AuthorId": 12,
        "EditorId": 12,
        "OData__UIVersionString": "1.0",
        "Attachments": false,
        "GUID": "9ceee022-a418-43d4-86b1-1de6d68edc47"
        }
   ]
}

2 个答案:

答案 0 :(得分:2)

从列表中删除项目时,我使用以下标题。

 "accept": "application/json;odata=verbose",
 "content-type": "application/json;odata=verbose",
 "X-RequestDigest": $("#__REQUESTDIGEST").val(),
 "If-Match": "*",
 "X-Http-Method": "DELETE".

对我而言,它有效。希望它对你也有帮助。

答案 1 :(得分:0)

归咎于错误导致标题错误的小错误(If-Match超出标题:这是正确的:

'delete': { method: 'DELETE', headers: { 'Accept': 'application/json;odata=verbose', 'content-type': 'application/json;odata=verbose', 'X-RequestDigest': $("#__REQUESTDIGEST").val(), 'IF-MATCH': '*' } }