From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/xmlhttprequest/readystate/index.html | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 files/zh-cn/web/api/xmlhttprequest/readystate/index.html (limited to 'files/zh-cn/web/api/xmlhttprequest/readystate/index.html') diff --git a/files/zh-cn/web/api/xmlhttprequest/readystate/index.html b/files/zh-cn/web/api/xmlhttprequest/readystate/index.html new file mode 100644 index 0000000000..c47030c9c8 --- /dev/null +++ b/files/zh-cn/web/api/xmlhttprequest/readystate/index.html @@ -0,0 +1,109 @@ +--- +title: XMLHttpRequest.readyState +slug: Web/API/XMLHttpRequest/readyState +tags: + - AJAX + - XMLHttpRequest +translation_of: Web/API/XMLHttpRequest/readyState +--- +

{{APIRef('XMLHttpRequest')}}

+ +

XMLHttpRequest.readyState 属性返回一个 XMLHttpRequest  代理当前所处的状态。一个 XHR 代理总是处于下列状态中的一个:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
状态描述
0UNSENT代理被创建,但尚未调用 open() 方法。
1OPENEDopen() 方法已经被调用。
2HEADERS_RECEIVEDsend() 方法已经被调用,并且头部和状态已经可获得。
3LOADING下载中; responseText 属性已经包含部分数据。
4DONE下载操作已完成。
+ +
+
UNSENT
+
XMLHttpRequest 代理已被创建, 但尚未调用 open() 方法。
+
OPENED
+
open() 方法已经被触发。在这个状态中,可以通过  setRequestHeader() 方法来设置请求的头部, 可以调用 send() 方法来发起请求。
+
HEADERS_RECEIVED
+
send() 方法已经被调用,响应头也已经被接收。
+
LOADING
+
响应体部分正在被接收。如果 responseType 属性是“text”或空字符串, responseText 将会在载入的过程中拥有部分响应数据。
+
DONE
+
请求操作已经完成。这意味着数据传输已经彻底完成或失败。
+
+ +
+

在IE中,状态有着不同的名称,并不是 UNSENTOPENEDHEADERS_RECEIVEDLOADING 和 DONE, 而是 READYSTATE_UNINITIALIZED (0),READYSTATE_LOADING (1) , READYSTATE_LOADED (2) , READYSTATE_INTERACTIVE (3) 和 READYSTATE_COMPLETE (4) 。

+
+ +

示例

+ +
var xhr = new XMLHttpRequest();
+console.log('UNSENT', xhr.readyState); // readyState 为 0
+
+xhr.open('GET', '/api', true);
+console.log('OPENED', xhr.readyState); // readyState 为 1
+
+xhr.onprogress = function () {
+    console.log('LOADING', xhr.readyState); // readyState 为 3
+};
+
+xhr.onload = function () {
+    console.log('DONE', xhr.readyState); // readyState 为 4
+};
+
+xhr.send(null);
+
+ +

规范

+ + + + + + + + + + + + + + +
规范状态注释
{{SpecName('XMLHttpRequest', '#states')}}{{Spec2('XMLHttpRequest')}}WHATWG living standard
+ +

浏览器兼容性

+ + + +

{{Compat("api.XMLHttpRequest.readyState")}}

+ +
 
-- cgit v1.2.3-54-g00ecf