SharePoint App无法使用Angular $资源将表单数据发布到REST

时间:2015-03-12 02:32:49

标签: angularjs rest sharepoint

我使用angular将表单数据发布到SharePoint的REST服务,但它没有发布。我能够"得到" JSON数据没有问题,相信我错过了一些明显的东西。我也没有收到错误消息。

以下是示例REST数据,我希望在"值"中创建另一个条目。对象:

{
"value": [
    {
        "FileSystemObjectType": 0,
        "Id": 1,
        "ContentTypeId": "0x010400AF4ACA1755AD48428EF182FA566973E6",
        "Title": "Test1",
        "Body": "<div class=\"ExternalClass5217CB82669B4C4B901FC90EF1D1734F\"><p>Test 1 body​</p></div>",
        "Expires": null,
        "RequesterId": null,
        "ManagerId": null,
        "ID": 1,
        "Modified": "2015-03-11T21:52:33Z",
        "Created": "2015-03-11T21:52:33Z",
        "AuthorId": 12,
        "EditorId": 12,
        "OData__UIVersionString": "1.0",
        "Attachments": false,
        "GUID": "e46af534-f488-4389-ad7e-726ed2af0d52"
    }
]

}

这是我的表单(控制器通过路由绑定到页面并且正在运行):

<form name="newItem" class="form-horizontal" data-ng-submit="createItem()" novalidate>
<fieldset>
     <div class="form-group">
        <label class="col-lg-2 control-label" for="title">Title *</label>
        <div class="col-lg-10">
            <input class="form-control" name="title" id="title" type="text" data-ng-model="itemtitle" placeholder="Add your title (Limited to 70 characters)" data-ng-maxlength="70" required>
            ({{70 - newItem.title.$viewValue.length}} Characters Remaining)
        </div>
    </div>

    <div class="form-group">
        <label class="col-lg-2 control-label" for="body">Body *</label>
        <div class="col-lg-10">
            <textarea class="form-control" name="body" id="body" data-ng-model="itembody" rows="4" placeholder="Add your body (Limited to 500 characters)" data-ng-maxlength="500" required> </textarea>
            Your summary will be displayed as follows ({{500 - newItem.body.$viewValue.length}} Characters Remaining):<br /> {{itembody}}

        </div>
    </div>
    <div class="form-group">
        <div class="col-lg-10 col-lg-offset-2">
            <button class="btn btn-default" data-ng-click="cancel()">Cancel</button>
            <button class="btn btn-primary" data-ng-disabled="newItem.$invalid">Submit</button>
            <p data-ng-show="newItem.$invalid" class="help-block validsubmit">The submit button will become "active" once all fields are properly filled out.</p>
        </div>
    </div>
</fieldset>

这是我的控制器:

// Add new item
appControllers.controller('appItemPostCtrl', ['$scope', '$location', 'appItems', function ($scope, $location, appItems) {

var itemEntry = new appItems;

$scope.createItem = function () {

    itemEntry.Title = $scope.itemtitle;
    itemEntry.value.$save();
    $location.path('/');
}

$scope.cancel = function () {
    $location.path('/');
}

}]);

这是我的服务(我想知道我是否需要&#34;保存&#34;):

appServices.factory('appItems', ['$resource', function ($resource) {
    return $resource("/_api/web/lists/getbytitle('Todo Task List')/Items", {},
    {
        '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() } },
    }
    );
}]);

编辑:我已经为我的POST方法添加了&#34; X-RequestDigest&#34;:$(&#34; #__ REQUESTDIGEST&#34;)。val()标头,但它仍然无法正常工作

1 个答案:

答案 0 :(得分:1)

使用$resource的有趣模式,我没有看到它像以前那样使用过。我认为您可以致电POST的原因是因为$save无法使用get()来处理未归还的商品。

尝试:

appItems.save({title: $scope.itemtitle})

相关问题