Can I output/flush data to screen while processing ajax page?
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) pag开发者_运维知识库e 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);
}
Essentially you will need to create a loop with server-side code that outputs a Javascript function call to update the result. Those requests must be made in a parallel HTTP connection.
More detail is available here: http://ajaxpatterns.org/HTTP_Streaming
精彩评论