I'm generating array of objects in JS, then sending it to PHP with $.ajax (using 'JSON.stringify(myData)')
.
And it works fine when that array has 50 objects inside (JSON.length == 2401), but it returns "error" if array has 77 objects (JSON.length == 3709)
Objects structure:
{ user_id: "27", hours: "2", service_id: "561" }
Why does that "error" happen?
When I try to send that long JSON I get 'access' entry with '000' code in domain journal without any data.
The JSON string is valid, checked it with online json decoder.
Code that sends AJAX:
requestReport() {
this.makeObjectForAjax();
const requestString = this.entriesForAjax;
const auth = $('#auth').text();
console.log(requestString);
$.ajax({
url: "/bitrix/TimeSheet/generateReport.php",
data: {
'data' : requestString,
'auth' : auth
},
success: function (data, textStatus) {
console.log(data);
console.log(textStatus);
},
error: function (data, textStatus, thrown) {
console.log(data);
console.log(textStatus);
console.log(thrown);
}
});
}
So that $.ajax
executes 'error: function', 'textStatus' == 'error', 'thrown' is empty string
That method first makes an array from raw data that JS gets, then it makes JSON
makeObjectForAjax() {
let entriesArray = [];
this.rawEntries.forEach(function (record) {
let newObj = {
'user_id' : Object.values(record.PROPERTY_117)[0],
'hours' : Object.values(record.PROPERTY_119)[0],
'service_id' : Object.values(record.PROPERTY_275)[0],
};
entriesArray.push(newObj);
});
this.entriesForAjax = JSON.stringify(entriesArray);
}
console.log(entriesArray) shows that array of objects is valid
Online JSON decoders say that string from
this.entriesForAjax = JSON.stringify(entriesArray);
Is valid too
UPD: solved! The problem was that $.ajax
makes GET request by default. Added type: 'POST'
to it and now it works!
Thanks to @Phil for comment about that