XMLHttpRequest 中的不同就绪状态意味着什么,以及如何使用它们?
XMLHttpRequest
有 5 个 readyState
,我只使用其中 1 个(最后一个,4
)。
其他的有什么用途,我可以在哪些实际应用中使用它们?
XMLHttpRequest
has 5 readyState
s, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
原始权威文档
0
、1
和2
仅跟踪到目前为止您已调用的发出请求所需的方法数量。3
告诉您服务器的响应已开始传入。但是当您从网页使用XMLHttpRequest
对象时,您几乎无能为力(*)该信息,因为您无权访问允许您读取部分数据的扩展属性。ReadyState
4
是唯一具有任何意义的状态。(*:我能想到的检查readyState
3
的唯一可能的用途是它在服务器端发出某种形式的生命信号,因此您可能会增加等待的时间当您收到完整回复时。)Original definitive documentation
0
,1
and2
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 theXMLHttpRequest
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.)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
可用。
(来自 https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState)
are available.
(From https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState)
kieron 的答案包含 w3schools 参考。 无人依赖,
bobince 的答案给出了 link ,它实际上告诉了 IE 的本机实现,
所以这里是引用的原始文档,以正确理解什么就绪状态代表:
请阅读此处: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 :
Please Read here : W3C Explaination Of ReadyState
readyState
值的完整列表为:(来自 https://www. w3schools.com/js/js_ajax_http_response.asp)
实际上,除了 4 之外,您几乎从不使用其中任何一个。
某些 XMLHttpRequest 实现可能会让您在
responseText< 中看到部分收到的响应/code> 当
readyState==3
时,但这并未得到普遍支持,因此不应依赖。The full list of
readyState
values is:(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
whenreadyState==3
, but this isn't universally supported and shouldn't be relied upon.