Chinaunix首页 | 论坛 | 博客
  • 博客访问: 36473
  • 博文数量: 7
  • 博客积分: 156
  • 博客等级: 入伍新兵
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-20 20:16
文章分类

全部博文(7)

文章存档

2011年(7)

分类: Python/Ruby

2011-06-02 12:43:22

大多数javascript初学者都只知道XMLHttpRequest请求时,readyState == 4代表请求的数据已经完全可用了,但是对0,1,2,3,4等值代表的具体含义,不一定完全了解了,同时,国内关于javascript方面的书籍、文章,鱼龙混杂,互相冲突,那究竟不同的readyState代表的具体含义是什么呢?查阅了最权威的w3c官方文档,终于找到了结果:
    文档,对readyState的描述如下:
    readyState of type unsigned short, readonly
The state of the object. The attribute MUSt be one of the following values:
    0 Uninitialized
The initial value.
    1 Open
The  method has been successfully called.
    2 Sent
The UA successfully completed the request, but no data has yet been received.
    3 Receiving
Immediately before receiving the message body (if any). All HTTP headers have been received. 
    4 Loaded
The data transfer has been completed. 
    但是,这并不是最终的答案,我们注意到,这份文档标记为W3C Working Draft 05 April 2006,在该文档标题下方,标记有Latest version:,标记日期为W3C Working Draft 19 November 2009,也就是说,前面的描述不是最新的,那最新的文档怎么描述的两者是否相同呢?答案是否定的,内容如下:
    The  object can be in several states. The readyState attribute, on getting, must return the current state, which must be one of the following values:

    UNSENT (numeric value 0)

The object has been constructed.

    OPENED (numeric value 1)

The  method has been successfully invoked. During this state request headers can be set using  and the request can be made using the  method.

    HEADERS_RECEIVED (numeric value 2)

All HTTP headers have been received. Several response members of the object are now available.

    LOADING (numeric value 3)

The  is being received.

    DONE (numeric value 4)

The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).

    The  state has an associated send() flag that indicates whether the  method has been invoked. It can be either true or false and has an initial value of false.

    The  state has an associated error flag that indicates some type of network error or abortion. It can be either true or false and has an initial value of false.

    略加分析,我们可以发现,这两份文档对readyState == 0,1,4的描述基本相同,但对于2和3的描述是完全不同的旧文档中,readyState == 2时,只是已经完成了send()操作,并没有收到任何数据,readyState == 3只是收到了HTTP headers,这样,浏览器大多数时间应该是停在3上,等待数据体和最后的数据处理了,并且readyState == 3时,是不能够处理数据的,新文档中,去除了Sent状态,2状态时所作的事情基本等同于旧文档的2和3状态,即调用了send(),并且只收到了HTTP headers,同时强化了数据接收的状态描述,将数据接收分为了接收数据内容(readyState == [2, 3))和内容处理(readyState == [3, 4))(主要包括null处理,根据Content-Type读取和处理MIME类型,字符集转换和错误处理等工作),也就是说,如果您很清楚返回值的情况的话,那么当readyState == 3时,服务器返回的数据已经完全传输完毕,您就可以自己开始处理数据了。
阅读(2837) | 评论(0) | 转发(1) |
0

上一篇:ubuntu编译安装mysql

下一篇:Bash Shell 快捷键

给主人留下些什么吧!~~