I am trying to retrieve a large amount of data from my server via a JQuery ajax GET request. The returned data is truncated (see image below). When I make the same exact request via my web browser by navigating directly to the URL, I get the full response, which is a JSON object (see image below).
I initially set the dataType option in my ajax call to “json”, but I switched it to “text” because the ajax error function was being triggered (couldn’t parse truncated json). Now it calls the success function, despite the fact that it hasn’t finished receiving the data.
A couple more points worth noting: Occasionally it will work and load all of the data, but still fails most of the time. I’m using a Node.js/Express server and res.json()
to send the response object.
Code:
$.ajax({
dataType: "text",
url: myURL,
method: "GET",
success: (data) => { resolve(data); },
error: (err) => { reject(err); },
timeout: 1000000
})
3
Answers
I had the same problem some times ago.
The problem was originated by a wrong
Content-Length
set by the API server.Hope this helps.
try to set at your API server, add php function
set_time_limit($second);
I think this is basically because of insufficient encoding of the data you must try
encodeURIComponent()
,by this function each instance of certain characters will be replaced by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two “surrogate” characters).Most of the errors(such as truncated data) are resolved by this encoding because as per the documentation the
$.ajax
function will only process approximately 50 characters of the return string, ignoring the rest.And you can even test this by decreasing the number of characters in your returned data.
To make it short you can try for
Here are some resources by which you can relate your problem
Jquery Fixes
Client Side Solution
JavaScript AJAX variables truncation issue FIX