Loading a file on the server - timing issues?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joe

    Loading a file on the server - timing issues?

    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("htt p://wwww.mysite.com/test.bin");
    mystr=xmlhttp.s tatusText;
    ...


    function loadXMLDoc(url)
    {
    xmlhttp=null;
    if (window.XMLHttp Request)
    {// code for IE7, Firefox, Opera, etc.
    xmlhttp=new XMLHttpRequest( );
    }
    else if (window.ActiveX Object)
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
    }
    if (xmlhttp!=null)
    {
    xmlhttp.onready statechange=sta te_Change;
    xmlhttp.open("G ET",url,true) ;
    xmlhttp.send(nu ll);
    }
    else
    {
    alert("Browser not supported.");
    }
    }

    function state_Change()
    {
    if (xmlhttp.readyS tate==4)
    {// 4 = "loaded"
    if (xmlhttp.status !=200)
    {
    alert("Problem: " + xmlhttp.statusT ext);
    }
    }
    }
  • Martin Honnen

    #2
    Re: Loading a file on the server - timing issues?

    joe wrote:
    loadXMLDoc("htt p://wwww.mysite.com/test.bin");
    mystr=xmlhttp.s tatusText;
    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
    onreadystatecha nge handler.


    --

    Martin Honnen

    Comment

    • joe

      #3
      Re: Loading a file on the server - timing issues?

      Martin Honnen <mahotrash@yaho o.dewrote:
      >joe wrote:
      >
      >loadXMLDoc("ht tp://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
      >onreadystatech ange handler.
      Thanks. It helped.

      Comment

      Working...