jQuery ajax: How to display partial server content.
I have a webpage which handle a time-consuming backend processing on the server side. I am hoping to use ajax and display the back-end processing process on the web-front(i.e. in a DIV). .ajaxStart and .ajaxStop sound to be OK if I want to display some fixed content. Is it possible that I grab partial server-side resultset and update a DIV at the front-end without having to wait for the server-side to finish all of the processing.
many thanks,
Hao
Below is my sample code which does NOT work for my purpose.
----
<span id="processing_status" class="blink"></span>
<div id="processing_log" style="display:none"></div>
<script type="text/javascript">
/* This is OK, but I want to display server-side processing records */
$('span#processing_status').ajaxStart(function() {
$(this).html('Processing...');
}).ajaxStop(function() {
$(this).html('');
}).show();
$('img#process').click(function() {
/* This is not working, only the final results displayed */
$.ajax({
url: '/path/to/processing_log.html',
data: {......},
async: true,
success: function(data) {
show_processing_log(data);
}
});
});
/* Each processing records is enclosed with a <DIV />, show only the last 10 records */
function show_processing_log(data) {
var OUT = data.split(/<\/div>\s*<div/);
if (OUT.length > 10) {
OUT.splice(0, OUT.length - 10);
}
var out_string = OUT.join('<\div><div');
$('div#process_log').html(out_string);
}
</script>