Ajax to read updated values from XML
Posted
by punit
on Stack Overflow
See other posts from Stack Overflow
or by punit
Published on 2010-05-26T16:49:52Z
Indexed on
2010/05/26
16:51 UTC
Read the original article
Hit count: 325
I am creating file upload progress bar. I have a CGI script which copies the data, and here I increment the progress bar value by ONE after certain iterations. I am storing the incremented value in XML file (I also tried using plain text file). On the other side I have ajax reading incremented value from xml and depending on that it increments the DIV element.
However, what happens here is, it seems to me that although the ajax reads all the incremented values but it processes it after the CGI has finished execution. That is progress bar starts execution once the file copying and other stuff in CGI is completed. My code is:
AJAX::::
function polling_start() { //GETS CALLED WHEN USER HITS FILE UPLOAD BUTTON intervalID = window.setInterval(send_request,100); } window.onload = function (){ request = initXMLHttpClient(); request.overrideMimeType('text/xml'); progress = document.getElementById('progress'); }
function initXMLHttpClient() { if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else{ // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp }
function send_request() { request.open("GET","progress_bar.xml",true); request.onreadystatechange = request_handler; request.send(); }
function request_handler() { if (request.readyState == 4 && request.status == 200) { var level=request.responseXML.getElementsByTagName('PROGRESS')[0].firstChild; progress.style.width = progress.innerHTML = level.nodeValue + '%'; progress.style.backgroundColor = "green"; } }
/****ON SERVER SIDE*********/
char xmlDat1[] = "<DOCUMENT><PROGRESS>";
char xmlDat2[] = "</PROGRESS></DOCUMENT>";
fptr = fopen("progress_bar.xml", "w");
.........OTHER STUFF.............................. ................................. if(i == inc && j<=100) { fprintf(fptr, "%s\n", "\n\n\n]>"); //fprintf(fptr, "%s\n", ""); fprintf(fptr, "%s", xmlDat1); // fprintf(fptr, "%d" ,j); fprintf(fptr, "%s" ,xmlDat2); fseek(fptr, 0, SEEK_SET); /*fprintf(fptr, "%d" ,j); fseek(fptr, 0, SEEK_SET);*/ i = 0; //sleep(1); j++; }
(I also tried to write in .text, but same response)
Any quick response would be appreciable.
© Stack Overflow or respective owner