I have a little 'incidental' question:
When sending a request with Ajax... open has to antecede send, for sure... but what about the onreadystatechange allocation?
Logically set before open-ing the Request object, after send-ing or in between?
To get'em back'n'ya mind:
// Using POST
XMLHttp.open('POST', url, true);
XMLHttp.onreadystatechange = RequestState;
XMLHttp.send(parameter);
Generally speaking, if the only readyState that you care about is 4, then it doesn't really make a difference if the onreadystatechange event handler is assigned prior to calling open(), in between open() and send(), or after calling send(). Here are the possible values for readyState:
- 0 - Uninitialized. The open() method hasn’t been called yet.
- 1 - Open. The open() method has been called but send() has not been called.
- 2 - Sent. The send() method has been called but no response has been received.
- 3 - Receiving. Some response data has been retrieved.
- 4 - Complete. All of the response data has been retrieved and is available.
By defining onreadystatechange before the open method is invoked, it is able to detect every state change from 0 through 4. If it is defined after the open method, then only states 1 through 4 will be detected. For this reason, it is generally preferred to place the onreadystatechange assignment before open().
One caveat to note is that when onreadystatechange was introduced in Internet Explorer 7, you had to set the event handler after calling open, otherwise it would cause an error. But this has been fixed in later versions of IE.
来源:https://stackoverflow.com/questions/16848955/ajax-onreadystatechange-open-send-order-totally-arbitrary