Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Loading a file on the server - timing issues?

Reply
Thread Tools

Loading a file on the server - timing issues?

 
 
joe
Guest
Posts: n/a
 
      04-25-2008
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);
}
}
}
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-25-2008
joe wrote:

> 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.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
joe
Guest
Posts: n/a
 
      04-25-2008
Martin Honnen <(E-Mail Removed)> wrote:

>joe wrote:
>
>> 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.


Thanks. It helped.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event/Timing problem when loading data from xml... ezmiller Javascript 1 01-09-2006 01:20 PM
Event/Timing problem when loading data from xml... ezmiller Javascript 0 01-09-2006 12:12 AM
Event/Timing problem when loading data from xml... ezmiller Javascript 0 01-09-2006 12:12 AM
ASP.NET SQL Server Session not timing out =?Utf-8?B?VGVqYXN3aQ==?= ASP .Net 1 02-12-2005 12:53 AM
Email server timing out in email script John Silver Perl 0 02-04-2005 07:47 AM



Advertisments