skip to Main Content

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
}) 

JQuery ajax call doesn't wait to receive entire response, resulting in truncated data.

3

Answers


  1. 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.

    Login or Signup to reply.
  2. try to set at your API server, add php function set_time_limit($second);

    Login or Signup to reply.
  3. 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

    $.ajax({
        dataType: "text",
        url: myURL,
        method: "GET",
        success: (data) => { resolve(encodeURIComponent(data)); },
        error: (err) => { reject(err); },
        timeout: 10000
    }) 
    

    Here are some resources by which you can relate your problem

    Jquery Fixes

    Client Side Solution

    JavaScript AJAX variables truncation issue FIX

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search