Ajax > onreadystatechange - open/send > order totally arbitrary?

前端 未结 1 802
悲哀的现实
悲哀的现实 2021-02-11 01:47

I have a little \'incidental\' question:

When sending a request with Ajax... open has to antecede send, for sure... but what about the

1条回答
  •  梦毁少年i
    2021-02-11 02:29

    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.

    0 讨论(0)
提交回复
热议问题