SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy XMLHTTP stuck in readystate 2

    Hi,

    I desprately need some help.

    I have some javascript code which basically loops through an array does a http request to get an XML file, recieves the file, stores it in an XML object and then moves onto the next loop.

    My code looks like

    function getItem(URL)
    {
    var downLoadHTTP = new ActiveXObject("Microsoft.XMLHTTP");
    downLoadHTTP.open('GET', URL, true);
    downLoadHTTP.onreadystatechange = IFSItemLoaded;
    downLoadHTTP.send(null);
    }

    function IFSItemLoaded()
    {
    if (downLoadHTTP.readyState != 4)
    return;

    ... do storing code here
    }

    The problem is that every now and then one of the files will just not download. If i put an alert in to check the readyState it will get to 2 but no further.

    I've used a sniffer program and it appears that the request is making it back to the PC but it just doesn't fire the onreadystatechange event.

    Has anyone every seen this problem before?? I'm majorly stuck.

    Thanks
    Mike

  2. #2
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of your test != I think you should try the reverse test
    PHP Code:
    function IFSItemLoaded()
    {
    if (
    downLoadHTTP.readyState == 4){
    .. do 
    storing code here


  3. #3
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You declare downLoadHTTP as a local variable inside getItem.
    Inside IFSItemLoaded, it will be null.

    Two easy possibilities change the line in IFSItemLoaded to read:
    Code:
    if (this.readyState != 4)
    or

    Code:
      ...
      downLoadHTTP.onreadystatechange = function() { IFSItemLoaded(downLoadHTTP); };
      downLoadHTTP.send(null);
      }
      
      function IFSItemLoaded(downLoadHTTP)
      {
    ...
    Only dead fish go with the flow

  4. #4
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oups missed that one ...
    another simple possibility would have been to declare downLoadHTTP as a global variable ..

  5. #5
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpaceFrog
    oups missed that one ...
    another simple possibility would have been to declare downLoadHTTP as a global variable ..
    Yep, it surely would. It is, however, bad programming practice, and Not Recommended.

    I'm not saying that you should never use them (some people do say that) - there are times when their use is highly appropriate. Using them as a knee-jerk reaction, however, leads to the Dark Path, and the chance of your code deserving a place on thedailywtf (where a frighteningly high proportion of Javascript code actually belongs).
    Only dead fish go with the flow

  6. #6
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have an advice on for what global variables should be reserved then ... ?
    I currently use global variables and have never encountered any problems with them...

  7. #7
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad when i cut and pasted the code. downLoadHTTP is actually declared globally at the top of my program.

    I don't think changing the test will help because if i put an alert in above the test all i ever get is a status of 2. I've even tried putting a button on the screen which shows the current status and again that only ever returns me 2.

    thanks for your help

  8. #8
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've just reread the original message.

    Need to know a bit more about the overall code - in your main loop, do you ever start a new httprequest before the last one has finished? That would have the effect of cancelling the previous request.
    Only dead fish go with the flow

  9. #9
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I never recreate the HTTP request object. Its created once at the start and then never again.

    The only difference in what i posted and my actual code is i have an array of file name eg

    Code:
    var myArray = new Array('a.xml', 'b.xml');
    then at the end of the onreadystatechange event i have some code like

    Code:
      count++;
      if (count < myArray.length)
      {
        getItem(myArray[count]);
      }
    the storing code just puts the returned XML into various different XMLDOM objects.

    cheers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •