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?