0

I met a very strange problem.

Short description: I have aspx page with AngularJS and have a List. Implemented 3 types of requests in AngularJS: 1. Get 2. POST 3. PATCH Everything works great except patch. Patch works, but it's too slow. And after Patch other request are not working sometimes. But if you reload the page the queries are working again.

var httpService = function ($http, $q) {
    var getRequest = function (url) {
        var deferred = $q.defer();
        $http({
            url: url,
            method: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
                "content-Type": "application/json;odata=verbose",
                "Cache-Control": "no-cache, no-store"
            }
        }).then(function (result) {
            deferred.resolve(result);
        }, function (result) {
            deferred.reject(result.status);
        });
        return deferred.promise;
    };
    var postRequest = function (data, url) {
        var deferred = $q.defer();
        $http({
            url: url,
            method: "POST",
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
                "content-Type": "application/json;odata=verbose"
            },
            data: JSON.stringify(data)
        }).then(function (result) {
            deferred.resolve(result);
        }, function (result) {
            deferred.reject(result.status);
        });
        return deferred.promise;
    };
    var updateRequest = function (data, url) {
        var deferred = $q.defer();
        $http({
            url: url,
            method: "POST",
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
                "content-Type": "application/json;odata=verbose",
                "X-HTTP-Method": "MERGE",
                "IF-MATCH": "*",
                "Cache-Control": "no-cache, no-store"
            },
            data: JSON.stringify(data)
        }).then(function (result) {
            deferred.resolve(result);
        }, function (result) {
            deferred.reject(result.status);
        });
        return deferred.promise;
    };


    return {
        getRequest: getRequest,
        postRequest: postRequest,
        updateRequest: updateRequest
    };
}

angular
    .module("myApp")
    .factory("httpService", ["$http", "$q", httpService]);

var form2Controller = function ($scope, formItem, form2Helper, hasAccess, progressBar, emailPreviewService) {

    formItem.getAll().then(function (users) {
        $scope.users = users;
    },
    function (message) {
        console.log(message);
    });

    $scope.disableSaveBtn = false;
    $scope.form2 = formItem.createEmpty();



    $scope.save = function () {
        $scope.disableSaveBtn = true;
        if ($scope.form.$valid) {
                $scope.form2.save($scope.form.event.$modelValue.rules).then(function () {                        
                    formItem.getAll().then(function (users) {
                        $scope.disableSaveBtn = false;
                        $scope.users = users;                            
                    },
                    function (message) {
                        console.log(message);
                        $scope.disableSaveBtn = false;
                    });
                }, function () {
                    $scope.disableSaveBtn = false;
                });

        } else {
            $scope.disableSaveBtn = false;
        }
    }

}

angular
    .module('myApp')
    .controller('form2Controller',
        ['$scope', 'formItem', 'form2Helper', 'hasAccess', 'progressBar', 'emailPreviewService', form2Controller]);

and $scope.form2.save look like:

            save: function (rules) {
            var deferred = $q.defer();
            this.setRules(rules);
            var data = {
                __metadata: { 'type': 'SP.Data.FormDataListListItem' },
                FieldData: JSON.stringify(this.FieldData)
            };
            if (this.ID != undefined) {
                var url = apiUrl + '/Items(' + this.ID + ')';
                var log_id = this.ID;
                httpService.updateRequest(data, url).then(function (data) {
                    deferred.resolve();
                }, function (status) {
                    console.error('Ошибка редактирования записи с id=' + log_id + '. ' + status);
                    deferred.reject('Ошибка редактирования записи с id=' + log_id + '. ' + status);
                });
            } else {
                var url = apiUrl + '/Items';
                httpService.postRequest(data, url).then(function (data) {
                    deferred.resolve();
                }, function (status) {
                    console.error('Ошибка сохранения записи ' + data + '. ' + status);
                    deferred.reject('Ошибка сохранения записи ' + data + '. ' + status);
                });
            }
            return deferred.promise;
        },

In the save method after creating an entry, I'm looking at whether it was created or not. If it was created, then I need to update the list items now, and load all the records, and for this I call the method get. That's the difference. If I create a new record (post), then downloading all the records works (get). But if I edit some record (patch with status 204), then after downloading all the records returns the status 200 but without data. How it can be?

0

I could not find a solution. Apparently this is such a feature of updating the elements. Now I solved this problem with the help of the client object model. https://msdn.microsoft.com/ru-ru/library/office/hh185011(v=office.14).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.