[development] Bug in HTTPGet/HTTPPost?

Chris Johnson chris at tinpixel.com
Thu May 18 21:22:42 UTC 2006


Justin wrote:
> 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>

This looks right to me.  The AJAX code I use in my web app uses the same 
open(), readystatchange, send() order.


More information about the development mailing list