Loading a file on the server - timing issues?

J

joe

I am loading a text file to a variable with XMLHttpRequest()
There seems to be some sort of timing issue since loadXML (source code below)
returns the contents of the file on seconds try. In Firefox I get an empty
string. No errors are reported.

If I look at the code with Firefox debugger (Venkman) everything works fine. I
should probably put a loop somewhere to check when loading the file is finished.


var xmlhttp;
...
[a lot of Javascipt code here]
...
loadXMLDoc("http://wwww.mysite.com/test.bin");
mystr=xmlhttp.statusText;
...


function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Browser not supported.");
}
}

function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status!=200)
{
alert("Problem:" + xmlhttp.statusText);
}
}
}
 
M

Martin Honnen

joe said:
loadXMLDoc("http://wwww.mysite.com/test.bin");
mystr=xmlhttp.statusText;

As your loadXMLDoc does asynchronous loading (third argument to open
method is true) you can't access the statusText in your code following
the loadXMLDoc call as that code is executed before the response to the
HTTP request will have been received.
Continue to use asynchronous loading but put any code trying to read
status, statusText and responseXML or responseText into the
onreadystatechange handler.
 
J

joe

Martin Honnen said:
As your loadXMLDoc does asynchronous loading (third argument to open
method is true) you can't access the statusText in your code following
the loadXMLDoc call as that code is executed before the response to the
HTTP request will have been received.
Continue to use asynchronous loading but put any code trying to read
status, statusText and responseXML or responseText into the
onreadystatechange handler.

Thanks. It helped.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top