0

I need to return a large amount of json data via an ajax call. Is there a built in jquery or javascript function to handle "chunking" of the data

ie: I need to be able to handle the data as it is returned by keeping the ajax call open and receiving chunks of data as it is sent from the server.

tbdevmanager
  • 373
  • 5
  • 15
  • 1
    http://stackoverflow.com/questions/6789703/how-to-write-javascript-in-client-side-to-receive-and-parse-chunked-response-i – marekful Nov 21 '13 at 13:59

1 Answers1

0

One method might be a self referencing ajax polling function something like...

 (function getData() { setTimeout(function() {
      $.ajax({ 
           url: "locationofserver", 
           success: function(data){

                // handle data returned (append chunks?)

                // get next bit
                getData();
           }, 
      dataType: "json"});
 }, 20000);
 })();

Where the first call returns information about the data length and how many chunks are available. This of course means the server needs to manage the breaking up of the data into chunks...

I would ask why you would need to chunk it though instead of just ensuring a persistent ajax connection until done? If you are truly looking to handle a data stream then maybe http://signalr.net/ or other push technology?

LFN
  • 97
  • 1
  • 5
  • I need to chunk it because the data is returned from a database query in one lump sum based on dates so I need the server to break up the dates and only ask for a specific set of data from the database otherwise there will be no data returned until the entire dataset is returned – tbdevmanager Nov 21 '13 at 14:41
  • Can you not only query the database for the data you need (for a given date range for example). If you are saying this is not possible then I would suggest you have no choice but to download everything and then query your dataset client side. Do have control of the database and server side logic? If so, what db type and technologies are you using. This really sounds like a server side solution is needed to me... Just for clarity - can you post your ajax call code to be sure we're talking about the same thing? – LFN Nov 21 '13 at 14:48
  • The situation is that a user will request a report for X number of days ie:30. So the number of records in the database is large for 30 days. So on the server side instead of querying the db for all 30 days of data break the request up to 7 day chunks from the db and once we get 7 days back send that to the users browser then keep the socket open and go get another 7 days send that back to the browser and keep going. I think I found a solution using JS XMLHttpRequest on the client side and having the CF Server return chunks of json which is processed with an onprogress event on the JS side – tbdevmanager Nov 22 '13 at 14:45