阿贾克斯>onreadystatechange-打开/发送>命令完全是任意的? [英] Ajax > onreadystatechange - open/send > order totally arbitrary?

查看:77
本文介绍了阿贾克斯>onreadystatechange-打开/发送>命令完全是任意的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个偶然的"问题:

I have a little 'incidental' question:

当使用 Ajax 发送请求时,... open 必须先行 send ,但是... onreadystatechange 分配?

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);

推荐答案

通常来说,如果您关心的唯一readyState是4,那么在调用之前分配onreadystatechange事件处理程序并不会真正起任何作用.open(),介于open()和send()之间,或在调用send()之后.这是readyState的可能值:

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-未初始化.尚未调用open()方法.
  • 1-打开.已调用open()方法,但未调用send().
  • 2-已发送.send()方法已被调用,但未收到响应.
  • 3-接收中.已检索到一些响应数据.
  • 4-完成.所有响应数据都已检索到并且可用.

通过在调用open方法之前定义onreadystatechange,它能够检测从0到4的每个状态更改.如果在open方法之后定义,则将仅检测到状态1到4.因此,通常最好将onreadystatechange分配放在open()之前.

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().

需要注意的一个警告是,在Internet Explorer 7中引入onreadystatechange时,您必须设置事件处理程序

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.

这篇关于阿贾克斯>onreadystatechange-打开/发送>命令完全是任意的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆