﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-憨厚生</title><link>http://www.blogjava.net/hulizhong/</link><description>----Java's Slave----&lt;br&gt;
***Java's Host***</description><language>zh-cn</language><lastBuildDate>Tue, 05 May 2026 11:10:09 GMT</lastBuildDate><pubDate>Tue, 05 May 2026 11:10:09 GMT</pubDate><ttl>60</ttl><item><title>readyState的五种状态详解【转】</title><link>http://www.blogjava.net/hulizhong/archive/2010/06/07/322942.html</link><dc:creator>二胡</dc:creator><author>二胡</author><pubDate>Mon, 07 Jun 2010 01:19:00 GMT</pubDate><guid>http://www.blogjava.net/hulizhong/archive/2010/06/07/322942.html</guid><wfw:comment>http://www.blogjava.net/hulizhong/comments/322942.html</wfw:comment><comments>http://www.blogjava.net/hulizhong/archive/2010/06/07/322942.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/hulizhong/comments/commentRss/322942.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hulizhong/services/trackbacks/322942.html</trackback:ping><description><![CDATA[<a href="http://www.cnblogs.com/myssh/archive/2009/12/18/1627368.html">【转】http://www.cnblogs.com/myssh/archive/2009/12/18/1627368.html</a><br />
<span class="Apple-style-span" style="word-spacing: 0px; font: medium 'Times New Roman'; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; webkit-text-size-adjust: auto; orphans: 2; widows: 2; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 13px; color: rgb(75,75,75); line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif">
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">在《Pragmatic <a title="" href="http://www.blogjava.net/hulizhong/articles/112412.html">AJAX中文问题</a>&nbsp;A Web 2.0 Primer 》中偶然看到对readyStae状态的介绍，感觉这个介绍很实在，摘译如下：</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">&nbsp;0: (Uninitialized) the send( ) method has not yet been invoked.&nbsp;<br />
&nbsp;1: (Loading) the send( ) method has been invoked, request in&nbsp;progress.&nbsp;<br />
&nbsp;2: (Loaded) the send( ) method has completed, entire response&nbsp;received.<br />
&nbsp;3: (Interactive) the response is being parsed.&nbsp;<br />
&nbsp;4: (Completed) the response has been parsed, is ready for harvesting.&nbsp;</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">&nbsp;0 －&nbsp;（未初始化）还没有调用send()方法<br />
&nbsp;1 －&nbsp;（载入）已调用send()方法，正在发送请求<br />
&nbsp;2 －&nbsp;（载入完成）send()方法执行完成，已经接收到全部响应内容<br />
&nbsp;3 －&nbsp;（交互）正在解析响应内容<br />
&nbsp;4 －&nbsp;（完成）响应内容解析完成，可以在客户端调用了</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">对于readyState的这五种状态，其他书中大都语焉不详。像《Foundations of <a title="" href="http://www.blogjava.net/hulizhong/articles/112412.html">AJAX中文问题</a>》中，只在书中的表2-2简单地列举了状态的&#8220;名称&#8221;－－The state of the request. The five possible values are 0 = uninitialized,&nbsp;1 = loading, 2 = loaded, 3 = interactive, and 4 = complete。而《Ajax in Action》中好像根本就没有提到这5种状态的细节。《Professional <a title="" href="http://www.blogjava.net/hulizhong/articles/112412.html">AJAX中文问题</a>》中虽不尽人意，但还是有可取之处：</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">There are five possible values for readyState:&nbsp;<br />
0 (Uninitialized): The object has been created but the open() method hasn&#8217;t been&nbsp;called.&nbsp;<br />
1 (Loading): The open() method has been called but the request hasn&#8217;t been sent.&nbsp;<br />
2 (Loaded): The request has been sent.&nbsp;<br />
3 (Interactive). A partial response has been received.&nbsp;<br />
4 (Complete): All data has been received and the connection has been closed.&nbsp;</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">readyState有五种可能的值：<br />
0 (未初始化)： (XMLHttpRequest)对象已经创建，但还没有调用open()方法。<br />
1 (载入)：已经调用open() 方法，但尚未发送请求。<br />
2 (载入完成)： 请求已经发送完成。<br />
3 (交互)：可以接收到部分响应数据。<br />
4 (完成)：已经接收到了全部数据，并且连接已经关闭。</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">在《<strong>Understanding <a title="" href="http://www.blogjava.net/hulizhong/articles/112412.html">AJAX中文问题</a>: Using JavaScript to Create Rich Internet Applications</strong>》中，则用下表进行了说明：</p>
<table cellspacing="0" align="center" border="1">
    <thead>
        <tr>
            <th valign="top" scope="col" align="left">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 24px">readyState Status Code</p>
            </th>
            <th valign="top" scope="col" align="left">Status of the<span class="Apple-converted-space">&nbsp;</span><tt>XMLHttpRequest</tt><span class="Apple-converted-space">&nbsp;</span>Object</th>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif">(0) UNINITIALIZED<br />
            未初始化</td>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif">The object has been created but not initialized. (The<span class="Apple-converted-space">&nbsp;</span><tt>open</tt><span class="Apple-converted-space">&nbsp;</span>method has not been called.)<br />
            （XMLHttpRequest）对象已经创建，但尚未初始化（还没有调用open方法）。</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><tt>(1) LOADING<br />
            载入</tt></td>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif">The object has been created, but the<span class="Apple-converted-space">&nbsp;</span><tt>send</tt><span class="Apple-converted-space">&nbsp;</span>method has not been called.<br />
            （XMLHttpRequest）对象已经创建，但尚未调用send方法。</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><tt>(2) LOADED<br />
            载入完成</tt></td>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif">The<span class="Apple-converted-space">&nbsp;</span><tt>send</tt><span class="Apple-converted-space">&nbsp;</span>method has been called, but the status and headers are not yet available.<br />
            已经调用send方法，（HTTP响应）状态及头部还不可用。</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><tt>(3) INTERACTIVE<br />
            交互</tt></td>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif">Some data has been received. Calling the<span class="Apple-converted-space">&nbsp;</span><tt>responseBody</tt><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><tt>responseText</tt><span class="Apple-converted-space">&nbsp;</span>properties at this state to obtain partial results will return an error, because status and response headers are not fully available.<br />
            已经接收部分数据。但若在此时调用responseBody和responseText属性获取部分结果将会产生错误，因为状态和响应头部还不完全可用。</td>
        </tr>
        <tr>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif"><tt>(4) COMPLETED<br />
            完成</tt></td>
            <td style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif">All the data has been received, and the complete data is available in the<span class="Apple-converted-space">&nbsp;</span><tt>responseBody</tt><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><tt>responseText</tt><span class="Apple-converted-space">&nbsp;</span>properties.<br />
            已经接收到了全部数据，并且在responseBody和responseText属性中可以提取到完整的数据。</td>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">根据以上几本书中的关于readyState五种状态的介绍，我认为还是《Pragmatic <a title="" href="http://www.blogjava.net/hulizhong/articles/112412.html">AJAX中文问题</a>&nbsp;A Web 2.0 Primer 》比较到位，因为它提到了对接收到的数据的解析问题，其他书中都没有提到这一点，而这一点正是&#8220;(3)交互&#8221;阶段作为一个必要的转换过程存在于&#8220;(2)载入完成&#8221;到&#8220;(4)完成&#8221;之间的理由，也就是其任务是什么。归结起来，我觉得比较理想的解释方法应该以&#8220;状态：任务（目标）+过程+表现（或特征）&#8221;表达模式来对这几个状态进行定义比较准确，而且让人容易理解。现试总结如下：</p>
<table cellspacing="0" cellpadding="5" rules="groups" align="center" border="1" frame="hsides">
    <thead>
        <tr>
            <th class="bottomBorder thead" valign="middle" scope="col" bordercolor="#ffffff" align="left" bgcolor="#6666cc">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 24px"><font color="#ffffff">readyState 状态</font></p>
            </th>
            <th class="bottomBorder thead" valign="middle" scope="col" bordercolor="#ffffff" align="left" bgcolor="#6666cc" height="35">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 24px"><tt><font color="#ffffff">状态说明</font></tt></p>
            </th>
        </tr>
        <tr>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px" align="left"><tt>(0)未初始化</tt></p>
            </td>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px">此阶段确认XMLHttpRequest对象是否创建，并为调用open()方法进行未初始化作好准备。值为0表示对象已经存在，否则浏览器会报错－－对象不存在。</p>
            </td>
        </tr>
        <tr>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left" bgcolor="#ffffcc">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px" align="left"><tt>(1)载入</tt></p>
            </td>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left" bgcolor="#ffffcc">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px">此阶段对XMLHttpRequest对象进行初始化，即调用open()方法，根据参数(method,url,true)完成对象状态的设置。并调用send()方法开始向服务端发送请求。值为1表示正在向服务端发送请求。</p>
            </td>
        </tr>
        <tr>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px" align="left"><tt>(2)载入完成</tt></p>
            </td>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px">此阶段接收服务器端的响应数据。但获得的还只是服务端响应的原始数据，并不能直接在客户端使用。值为2表示已经接收完全部响应数据。并为下一阶段对数据解析作好准备。</p>
            </td>
        </tr>
        <tr>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left" bgcolor="#ffffcc">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px" align="left"><tt>(3)交互</tt></p>
            </td>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left" bgcolor="#ffffcc">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px">此阶段解析接收到的服务器端响应数据。即根据服务器端响应头部返回的MIME类型把数据转换成能通过responseBody、responseText或responseXML属性存取的格式，为在客户端调用作好准备。状态3表示正在解析数据。</p>
            </td>
        </tr>
        <tr>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px" align="left"><tt>(4)完成</tt></p>
            </td>
            <td class="docTableCell" style="font-size: 12px; color: rgb(69,69,69); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif" valign="top" align="left">
            <p class="docText" style="margin: 5px auto; text-indent: 0px; line-height: 18px">此阶段确认全部数据都已经解析为客户端可用的格式，解析已经完成。值为4表示数据解析完毕，可以通过XMLHttpRequest对象的相应属性取得数据。</p>
            </td>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">概而括之，整个XMLHttpRequest对象的生命周期应该包含如下阶段：<br />
创建－初始化请求－发送请求－接收数据－解析数据－完成</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">在具体应用中，明确了readyState的五个状态（XMLHttpRequest对象的生命周期各个阶段）的含义，就可以消除对Ajax核心的神秘感（语焉不详的背后要么是故弄玄虚，制造神秘感；要么就是&#8220;以其昏昏，使人昭昭&#8221;），迅速把握其实质，对减少学习中的挫折感和增强自信心都极其有益。</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">比如，通过如下示例：</p>
<pre style="margin-top: 0px; margin-bottom: 0px">
<p class="code" style="margin: 5px auto; text-indent: 0px; line-height: 19px">
//声明数组
var states ＝ [&#8220;正在初始化&#8230;&#8230;&#8221;,
&#8220;正在初始化请求&#8230;&#8230;成功！
正在发送请求&#8230;&#8230;&#8221;,
&#8220;成功！
正在接收数据&#8230;&#8230;&#8221;,
&#8220;完成！
正在解析数据&#8230;&#8230;&#8221;,
&#8220;完成！
&#8221;];
//回调函数内部代码片段
if (xmlHttp.readyState==4)
{
var span = document.createElement(&#8220;span&#8221;);
span.innerHTML = states[xmlHttp.readyState];
document.body.appendChild(span);
if (xmlHttp.status == 200)
{
var xmldoc = xmlHttp.responseXML;
//其他代码
}
//别忘记销毁，防止内存泄漏
xmlHttp = null;
}else{
var span = document.createElement(&#8220;span&#8221;);
span.innerHTML = states[xmlHttp.readyState];
document.body.appendChild(span);
}</p>
</pre>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">结果如下：</p>
<p class="code" style="margin: 5px auto; text-indent: 0px; line-height: 19px">正在初始化请求&#8230;&#8230;成功！<br />
正在发送请求&#8230;&#8230;成功！<br />
正在接收数据&#8230;&#8230;完成！<br />
正在解析数据&#8230;&#8230;完成！</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">我们很容易明白XMLHttpRequest对象在各个阶段都在做什么。因此，也就很容易对Ajax的核心部分有一个真正简单明了的理解。</p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px"><span style="color: red; line-height: 19px">本博PS：readyState一般用在异步请求时程序响应的判断，Iframe， javaScript脚本同样适用,参考另一篇文章：</span><a style="color: rgb(26,139,200); text-decoration: none" href="http://d-tune.javaeye.com/blog/506074" target="_blank"><span style="color: red; line-height: 19px">http://d-tune.javaeye.com/blog/506074</span></a></p>
<p style="margin: 5px auto; text-indent: 0px; line-height: 19px">文章出处：<a style="color: rgb(26,139,200); text-decoration: none" href="http://www.cn-cuckoo.com/2007/07/16/the-details-for-five-states-of-readystate-9.html" target="_blank">http://www.cn-cuckoo.com/2007/07/16/the-details-for-five-states-of-readystate-9.html</a></p>
</span></span><img src ="http://www.blogjava.net/hulizhong/aggbug/322942.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hulizhong/" target="_blank">二胡</a> 2010-06-07 09:19 <a href="http://www.blogjava.net/hulizhong/archive/2010/06/07/322942.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>