XMLHttpRequest 中的不同就绪状态意味着什么,以及如何使用它们?

发布于 2024-07-15 01:04:43 字数 133 浏览 13 评论 0原文

XMLHttpRequest 有 5 个 readyState,我只使用其中 1 个(最后一个,4)。

其他的有什么用途,我可以在哪些实际应用中使用它们?

XMLHttpRequest has 5 readyStates, and I only use 1 of them (the last one, 4).

What are the others for, and what practical applications can I use them in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

毅然前行 2024-07-22 01:04:43

原始权威文档

012 仅跟踪到目前为止您已调用的发出请求所需的方法数量。

3 告诉您服务器的响应已开始传入。但是当您从网页使用 XMLHttpRequest 对象时,您几乎无能为力(*)该信息,因为您无权访问允许您读取部分数据的扩展属性。

ReadyState 4 是唯一具有任何意义的状态。

(*:我能想到的检查readyState 3的唯一可能的用途是它在服务器端发出某种形式的生命信号,因此您可能会增加等待的时间当您收到完整回复时。)

Original definitive documentation

0, 1 and 2 only track how many of the necessary methods to make a request you've called so far.

3 tells you that the server's response has started to come in. But when you're using the XMLHttpRequest object from a web page there's almost nothing(*) you can do with that information, since you don't have access to the extended properties that allow you to read the partial data.

readyState 4 is the only one that holds any meaning.

(*: about the only conceivable use I can think of for checking for readyState 3 is that it signals some form of life at the server end, so you could possibly increase the amount of time you wait for a full response when you receive it.)

挽清梦 2024-07-22 01:04:43

onreadystatechange 存储每次readyState属性改变时自动调用的函数(或函数名)
ReadyState 保存 XMLHttpRequest 的状态。 从 0 更改为 4:

0:请求未初始化

1:服务器连接已建立

2:已收到请求

3:正在处理请求

4:请求已完成且响应已准备好

状态 200:“确定”

404:未找到页面

onreadystatechange Stores a function (or the name of a function) to be called automatically each time the readyState property changes
readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:

0: request not initialized

1: server connection established

2: request received

3: processing request

4: request finished and response is ready

status 200: "OK"

404: Page not found

有深☉意 2024-07-22 01:04:43
  • 0:UNSENT 客户端已创建。 open() 尚未调用。
  • 1:已打开 open() 已被调用。
  • 2 : HEADERS_RECEIVED send() 已被调用,并且标头和状态
    可用。
  • 3:LOADING 正在下载; responseText 保存部分数据。
  • 4:DONE 操作完成。

(来自 https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState)

  • 0 : UNSENT Client has been created. open() not called yet.
  • 1 : OPENED open() has been called.
  • 2 : HEADERS_RECEIVED send() has been called, and headers and status
    are available.
  • 3 : LOADING Downloading; responseText holds partial data.
  • 4 : DONE The operation is complete.

(From https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState)

筑梦 2024-07-22 01:04:43

kieron 的答案包含 w3schools 参考。 无人依赖,
bobince 的答案给出了 link ,它实际上告诉了 IE 的本机实现,

所以这里是引用的原始文档,以正确理解什么就绪状态代表:

XMLHttpRequest 对象可以处于多种状态。 ReadyState 属性必须返回当前状态,该状态必须是以下值之一:

未发送(数值 0)
对象已构建。

OPENED(数值1)
open() 方法已成功调用。 在此状态下,可以使用 setRequestHeader() 设置请求标头,并可以使用 send() 方法发出请求。

HEADERS_RECEIVED(数值 2)
所有重定向(如果有)均已遵循,并且最终响应的所有 HTTP 标头均已收到。 该对象的多个响应成员现在可用。

正在加载(数值3)
正在接收响应实体主体。

完成(数值 4)
数据传输已完成或传输过程中出现问题(例如无限重定向)。

请阅读此处:W3C ReadyState 解释

kieron's answer contains w3schools ref. to which nobody rely ,
bobince's answer gives link , which actually tells native implementation of IE ,

so here is the original documentation quoted to rightly understand what readystate represents :

The XMLHttpRequest object can be in several states. The readyState attribute 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 open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method.

HEADERS_RECEIVED (numeric value 2)
All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available.

LOADING (numeric value 3)
The response entity body is being received.

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

Please Read here : W3C Explaination Of ReadyState

把时间冻结 2024-07-22 01:04:43

readyState 值的完整列表为:(

State  Description
0      The request is not initialized
1      The request has been set up
2      The request has been sent
3      The request is in process
4      The request is complete

来自 https://www. w3schools.com/js/js_ajax_http_response.asp

实际上,除了 4 之外,您几乎从不使用其中任何一个。

某些 XMLHttpRequest 实现可能会让您在 responseText< 中看到部分收到的响应/code> 当 readyState==3 时,但这并未得到普遍支持,因此不应依赖。

The full list of readyState values is:

State  Description
0      The request is not initialized
1      The request has been set up
2      The request has been sent
3      The request is in process
4      The request is complete

(from https://www.w3schools.com/js/js_ajax_http_response.asp)

In practice you almost never use any of them except for 4.

Some XMLHttpRequest implementations may let you see partially received responses in responseText when readyState==3, but this isn't universally supported and shouldn't be relied upon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文