Can I output/flush data to screen while processing ajax page?
Posted
by Bee
on Stack Overflow
See other posts from Stack Overflow
or by Bee
Published on 2010-03-27T13:51:25Z
Indexed on
2010/03/27
13:53 UTC
Read the original article
Hit count: 366
I need to display on my page a list of records pulled from a table. Ajax works fine (I query the database and put all the data inside a on the main page) but if I have lots of records (say 500+) it will hang until data is fully loaded, THEN it will be sent back to the page and correctly displayed.
I would like to be able to display the records on the page while getting them, instead of being forced to wait until completion. I am trying with flush(); inside the remote (ajax) page but it still waits until full data is loaded.
This is what I currently have inside the ajax page:
At the very beginning:
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);
Then whenever I have a echo call:
ob_flush();
Now if I load the ajax page alone... it will list the records while reading them from the database. But if I call the same page via Ajax, it will hang and send all the data at once.
Any idea?
This is the function I use to get the ajax content ('id' is the target , 'url' refers to the ajax page that runs the database query to list the records):
function ajax(id,url) {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
document.getElementById(id).innerHTML = parseScript(xmlhttp.responseText);
}
© Stack Overflow or respective owner