[development] Bug in HTTPGet/HTTPPost?

Justin rocketfuel at spaceship.com
Thu May 18 20:55:18 UTC 2006


Greetings!

I'm working with the vote_up_down module and think that I may have
found a bug in HTTPGet()/HTTPPost() functions.



A simplified version of the code that is common to both functions:

<code>
  xmlHttp.open('GET', uri, bAsync);
  xmlHttp.send(null);

  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
      callbackFunction(xmlHttp.responseText, xmlHttp, callbackParameter);
    }
  }
</code>


The idea is that the onreadystatechange handler will allow a callback function
to be called asynchronously once the xmlHttp object is in state 4, "completed".

However, I believe it is possible that the xmlHttp object sometimes has already 
transistioned to state 4 before the code that sets the handler is executed.  
This would mean that the callback is never called because the state never changes 
(if it is already at "completed").

Reading the MS docs, I learned that it is the send() method which will update the
ready state, and so the handler should be set between the open() and send() calls.



Like so:

<code>
  xmlHttp.open('GET', uri, bAsync);

  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
      callbackFunction(xmlHttp.responseText, xmlHttp, callbackParameter);
    }
  }

  xmlHttp.send(null);
</code>



If there is no disagreement, I'd be happy to submit a patch for this change.  


Cheers,
 Justin




More information about the development mailing list