3

Anyone can help me. I'm working in Jquery-mobile with phonegap application. How can i append data at the same time getting ajax response from server? Here more than 800 kb data is retrieved from the server as Json format.

This is my code:

$.ajax({        
    url:url,
    data:'',
    contentType: "application/json; charset=utf-8",
    type: "POST",
    dataType: "json",
    crossDomain:true,
    cache: false,
    async:false,
    //success: loadAllCars,

    success:function(data){
       //alert(data)
    },
    error: OnError
}); 

I want to fetch data at the same time which getting from server for example:

I have more than 800 KB size of data and when it's get 10 KB size data of 800 KB and i need to fetch that at the same time for decrease the delay of appending and avoid the user waiting

Guru
  • 98
  • 7

2 Answers2

1

JSON would not be valid in this case. So you could not do this :(

In any case you can show loading progress.

How to show loading spinner in jQuery?

Community
  • 1
  • 1
Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55
1

JSON cannot be used because all the data objects are wrapped in {} and []. This means you have to wait to the end of the file until the parser knows what to do with it (which is how to construct objects).

Maybe you can use a different encoding, which sends all the data "as is"; de order is than important but there will be no "end" marks (such as } and ]).

Using the XMLHTTPRequest.onreadystatechange() method, which fires for every received block of data with a readyState of 3, you can read the XMLHTTPRequest.resonseText. Then you have to create a smart buffer yourself if you are sending objects, or directly use display the data if it is just text.

I don't think you can do this with the jQuery.ajax() method, but the direct method is not that difficult.

By the way; you also have to make sure your server flushes the data from time to time in case it has to use a lot of resources locally. For instance; when doing heavy computations, every results can be flushed to the client while the server processes for the next result.

Roemer
  • 3,566
  • 1
  • 16
  • 32
  • Can we get ajax requesting percent.? – Guru May 24 '13 at 07:24
  • @Amir, What do you mean with "percent" in the context of an ajax request? – Roemer May 24 '13 at 08:25
  • I mean... when we get response from server to client we can shows it in progress. for example its load 10% of complete data then we can shows as 10 out of 100 in progress Bar. – Guru May 24 '13 at 08:53
  • Sure.. if you know in advance how many bytes of data you are about to send. That number can be the first part of your custom-made format. Then the client knows how much to expect. On every `onreadystatechange()` event it can check how many bytes are already received, and do the math. – Roemer May 24 '13 at 09:05
  • ,Thanks for your prompt response :) – Guru May 24 '13 at 09:15
  • @ Flaxfield : But can we fetch the data by the format as you said in same time ? – ULLAS MOHAN.V May 24 '13 at 11:23
  • 1
    Yes. Well... the first few bytes are to say how many bytes will follow. Then with every received block of data you can process it (display it) and calculate how many accumulated bytes you have perceived so far. So you know the percentage. This only works if the server knows beforehand how much it will send (or the client has an expectation about how much it will receive) – Roemer May 24 '13 at 12:05