-1

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

Roman
  • 1
  • 3
  • 2
    Can you please share your code? – Kunal Dholiya Jul 23 '19 at 04:47
  • 2
    Also, what is the error? Where do you see it? What does the network inspector say happened? – Amadan Jul 23 '19 at 04:48
  • Is this any APIs or simple data fetched from your local db? – Kunal Dholiya Jul 23 '19 at 04:49
  • I guess it's not depend on you json length it about you json structure – errorau Jul 23 '19 at 04:51
  • @KunalDholiya shared it's data from bitrix24 API. I make new array of objects from raw data, then make json – Roman Jul 23 '19 at 05:07
  • @Amadan in $.ajax error:function, in console.log(textStatus) which is "error". It also console.log(thrown), but it's empty – Roman Jul 23 '19 at 05:07
  • 1
    Inspect the request itself in the [Network Inspector](https://developers.google.com/web/tools/chrome-devtools/network/#load). See what the headers are (both for request and response), and what the body of the response it. – Amadan Jul 23 '19 at 05:09
  • The problem is almost certainly server-side so what does your PHP error log say? – Phil Jul 23 '19 at 05:21
  • Just noticed you're performing a `GET` request. You'll quickly hit the [limits of maximum URL length](https://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string). I recommend using a `POST` request instead – Phil Jul 23 '19 at 05:27
  • @phil php doesn't receive any data. So it says nothing – Roman Jul 23 '19 at 05:27
  • @phil, ok, will try Post request – Roman Jul 23 '19 at 05:29
  • @Roman keep in mind, you may need to alter your PHP script – Phil Jul 23 '19 at 05:29
  • 1
    @phil problem solved just by adding ```type: 'POST``` to $.ajax. Thank you! – Roman Jul 23 '19 at 07:26

2 Answers2

1

Your dataType: "json" only tells jQuery that you want it to parse the returned JSON, it does not mean that jQuery will automatically stringify your request data.

$.ajax({
        type: "POST",
        url : "URL",
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify({
            user_id: 27,
            hours: 2,
            service_id: 561,
        }),
        success: function(response) {
            console.log(response);
        },
        error: function(response) {
            console.log(response);
        }
});
Jalodara Jayesh
  • 327
  • 3
  • 17
0

You aren't actually sending JSON. You are passing an object as the data, but you need to stringify the object and pass the string instead and also you missing type: either post or get

Your dataType: "json" only tells jQuery that you want it to parse the returned JSON, it does not mean that jQuery will automatically stringify your request data.

Change to:

$.ajax({
        type: 'POST',
        url: "/bitrix/TimeSheet/generateReport.php",
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify({
            '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);
        }
    });
jvk
  • 2,133
  • 3
  • 19
  • 28