我正在使用角度将表单数据发布到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('/');
}
}]);
这是我的服务(我想知道我是否需要"保存"):
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() } },
}
);
}]);
编辑:我已经添加了"X-RequestDigest": $("#__REQUESTDIGEST").val() 标头到我的 POST 方法,但它仍然不起作用
$resource
的有趣模式,我以前从未见过这样使用。我认为您可以调用POST
的原因是因为您不能对由于get()
而未返回的项目使用$save
。
尝试:
appItems.save({title: $scope.itemtitle})