Bug in HTTPGet/HTTPPost?
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
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.
On 18/05/06, Justin <rocketfuel@spaceship.com> wrote:
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.
I think onreadystate change should be set before open(). See http://www.w3.org/TR/XMLHttpRequest/#dfn-readystate
If there is no disagreement, I'd be happy to submit a patch for this change.
Go for it :) I'll also take this moment to say I'd still like the two functions to be rolled into one multi-purpose function - preferably by passing an object for all the options (similar to prototype.js). -- David Carrington
David Carrington wrote:
On 18/05/06, Justin <rocketfuel@spaceship.com> wrote:
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.
I think onreadystate change should be set before open(). See http://www.w3.org/TR/XMLHttpRequest/#dfn-readystate
Yeah, since it's a callback you're best to set it after you create the object, and before you actually start working with it. It's testing for readyState == 4, so any extra state changes won't negatively affect anything.
participants (4)
-
Chris Johnson -
David Carrington -
Justin -
Rowan Kerr