简体   繁体   English

使用angularjs REST HTTP POST到SharePoint列表

[英]REST HTTP POST to SharePoint List with angularjs

I'm using angularjs to create a simple SPA which allows users to interact with a Bookings list in Hosted SharePoint 2013. 我正在使用angularjs创建一个简单的SPA,允许用户与Hosted SharePoint 2013中的“预订”列表进行交互。

I've got the HTTP GET working fine and retrieving bookings for various filtered queries. 我的HTTP GET工作正常,可以检索各种过滤查询的预订。

However, now I want to Insert and Update records into the list, but Fiddler shows an 'HTTP Error 403' occurred and the following under JSON tab: 但是,现在我想将记录插入和更新到列表中,但是Fiddler显示发生了“ HTTP错误403”,并且在JSON选项卡下显示了以下内容:

value=The security validation for this page is invalid and might be corrupted. value =此页面的安全验证无效,并且可能已损坏。 Please use your web browser's Back button to try your operation again. 请使用网络浏览器的“后退”按钮再次尝试操作。

I would appreciate some help confirming the following code should work. 希望获得一些帮助,以确保以下代码可以正常工作。 A Submit button on new booking form triggers the post: 新预订表单上的“提交”按钮将触发该帖子:

$scope.submitForm = function () {
  //new booking
    if ($scope.editableBooking.Id == 0) {
        service.insertBooking($scope.editableBooking, function (data) {
            $scope.editableBooking = data;
        });
    }
  // Update booking
    else {
        console.log("[submitForm] Update");
        service.updateBooking($scope.editableBooking, function (data) {
            $scope.editableBooking = data;
            console.log('[updatedBooking] id = ' + $scope.editableBooking.Id)
        });
    }
}

Within my factory I have a service to insert booking: 在我的工厂内,我可以提供预订服务:

var insertBooking = function (newBooking, callback) {
    console.log('[insertBooking] Id = ' + newBooking.Id + " Storeno = " + newBooking.Storeno);

    $http({
        method: 'POST',
        url: "/_api/web/lists/GetByTitle('Bookings')",
        data: $.param({
            Title: newBooking.Storeno,
            Description: newBooking.Description,
            BookedBy: newBooking.Bookedby,
            StartDate: newBooking.StartDate
        }),
        headers: { 'Accept': 'application/json; odata=verbose' }
    }).success(function (data) {
        console.log("[insertBooking] POST worked");
        console.log('[insertbooking] New Id = ' + data.Id);

        callback(data);

    }).error(function (er) {
        console.log('[insertBooking] Error = ' + er);
    });
}

Searching StackOverFlow is this post on Error 403 which talks about the AppManifest. 搜索StackOverFlow是有关Error 403的帖子,其中讨论了AppManifest。 I don't have access to this file - it's on a corporate intranet - and I haven't built an installable app, just angularjs files called via a CEWP. 我没有访问此文件的权限-它在公司的Intranet上-并且我还没有构建可安装的应用程序,只是通过CEWP调用的angularjs文件。

Any suggestions please on how to update a SP list? 关于如何更新SP列表有任何建议吗?

I've had this same error in the past 我过去也有过同样的错误

The security validation for this page is invalid and might be corrupted. 此页面的安全验证无效,并且可能已损坏。 Please use your web browser's Back button to try your operation again. 请使用网络浏览器的“后退”按钮再次尝试操作。

I found this blog by Wictor Wilén that explains how to refresh the digest token. 我找到了WictorWilén撰写的博客 ,该博客解释了如何刷新摘要令牌。

Basically you insert this line before your $http invocation: 基本上,您在$http调用之前插入以下行:

UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval);

Some main points from the blog: 博客的一些要点:

  • This method is synchronous, so there's no need to worry about callbacks etc. 此方法是同步的,因此无需担心回调等。
  • it uses an update interval and only updates the form digest when needed – so if your digest hasn't expired, there are no additional calls to SharePoint 它使用更新间隔,并且仅在需要时更新表单摘要–因此,如果摘要尚未过期,则不会再有其他对SharePoint的调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM