如果接收到一个响应,那么请求对象会注册为就绪状态4。
If a response is received, the request object registers a ready state of 4.
当一个请求到达时,任务开始进入就绪状态。
您会看到就绪状态为3的响应在每个脚本、每个服务器甚至每个浏览器上都是不一样的。
You'll find that your responses in ready state 3 vary from script to script, server to server, and browser to browser.
要查看这种变化,请使用如 清单5所示的代码来测试请求的响应文本,以及它们的就绪状态。
To see this in action, use code like that shown in Listing 5 to test the response text of a request, as well as its ready state.
换句话说,服务器履行了请求(即HTTP就绪状态是4)但是没有返回客户机预期的数据。
In other words, the server fulfilled the request (meaning the HTTP ready state is 4), but is probably not returning the data expected by the client.
还有一些则多次报告就绪状态1。
所有的文档和规范都强调,只有在就绪状态为4时数据才可以安全使用。
All of the documentation and specifications insist that it's only when the ready state is 4 that data is safe to use.
其中的第一点,即就绪状态,将在下一篇文章中详细讨论,您将进一步了解HTTP请求的阶段,可能比您设想的还多。
The first of these — ready states — is going to take up the bulk of the next article; you'll learn more about the stages of an HTTP request than you ever wanted to know.
下面让我们首先来看一下HTTP就绪状态。
如果重新启用,错误路径将回到就绪状态。
The faulty path will go back to ready status if you re-enable it.
您也许期望任务就绪状态从0到1、2、3再到4,但实际上很少是这种情况。
You might expect to always see the ready state move from 0 to 1 to 2 to 3 to 4, but in practice, that's rarely the case.
另外,未初始化的就绪状态在实际的应用程序中是没有真正的用处的。
Furthermore, the uninitialized ready state is pretty useless in practical applications.
然而,由于服务器对请求进行了应答,因此就绪状态就被设置为4(即使应答并不是请求所期望的也是如此)。
However, the ready state will be set to 4 since the server answered the request (even if the answer wasn't what you wanted or expected for your request).
要更好地看到这段代码的效果,请使用Firefox或InternetExplorer,因为这两个浏览器都可以报告出请求过程中所有可能的就绪状态。
To get the most out of this code, use either Firefox or Internet Explorer since those two browsers both report all possible ready states during a request.
一旦我们理解在请求过程中发生的各个就绪状态之后,接下来就可以来看一下XMLHttpRequest对象的另外一个方面了——responsetext属性。
Once you understand the various ready states that occur during a request, you're ready to look at another important piece of the XMLHttpRequest object — the responseText property.
在很多Ajax应用程序中,您将看到一个回调函数,它负责检查就绪状态,然后继续利用从服务器响应中返回的数据,如清单6所示。
In many Ajax applications, you'll see a callback function that checks for a ready state and then goes on to work with the data from the server response, as in Listing 6.
在Opera上,您永远都不会看到前两个就绪状态,而在Safari上则没有第一个(1)。
On Opera, you'll never get those first two ready states and Safari drops the first one (1).
与就绪状态类似,responsetext属性的值在整个请求的生命周期中也会发生变化。
Like the ready state, the value of the responseText property changes throughout the request's life cycle.
不过为了满足我们的兴趣,请参见 清单2 的内容,其中显示了如何在 readyState被设置为 0 时来获取这种就绪状态。
Still, in the interest of being complete, check out Listing 2 which shows how to get the ready state when it's set to 0.
例如,在使用Opera 8.5时,所显示的就绪状态情况就更加糟糕了。
For example, when using Opera 8.5, things are even worse with displayed ready states.
查看正在处理的请求的就绪状态。
在大部分Ajax编程的真实情况中,这种就绪状态的唯一用法就是使用相同的XMLHttpRequest对象在多个函数之间生成多个请求。
The only use for this ready state in almost-real-world Ajax programming is if you make multiple requests using the same XMLHttpRequest object across multiple functions.
深入了解HTTP就绪状态。
Safari实际上把第一个就绪状态给丢弃了,也并没有什么明显的原因说明为什么要这样做;不过这就是Safari的工作方式。
Safari actually leaves out the first ready state and there's no sensible explanation as to why; it's simply the way Safari works.
然而,如果您不仅仅是想了解Ajax编程的常识,而是希望了解更多内容,就需要熟悉就绪状态、状态代码和请求本身的内容。
However, you will need to be fluent in ready states, status codes, and requests if you want to do more than just dabble in Ajax programming.
清单1相当简单;清单4稍微有点复杂,但是它们在开始时都要检查就绪状态,并获取responsetext属性的值。
Listing 1 is fairly simple; Listing 4 is a little more complicated, but to begin, both check the ready state and then grab the value (or values) in the responseText property.
在就绪状态发生变化后,将调用req . onreadystatechange属性中指定的函数(请参见清单17)。
After the ready state changes, the function specified in the req.onreadystatechange property is invoked (see Listing 17).
注意您必须在调用open之前来查看就绪状态。
Note that you've got to check the ready state before open is called.
请求的就绪状态发生变化时,req.onreadystatechange中指定的函数将被调用。
When the ready state of a request changes, the function specified in req.onreadystatechange is invoked. req.readystate contains one of the following status codes
请求的就绪状态发生变化时,req.onreadystatechange中指定的函数将被调用。
When the ready state of a request changes, the function specified in req.onreadystatechange is invoked. req.readystate contains one of the following status codes
应用推荐