XMLHttpRequest.readyState

翻譯不完整。請協助 翻譯此英文文件

The XMLHttpRequest.readyState property returns the state an XMLHttpRequest client is in. An XHR client exists in one of the following states:

Value State Description
0 UNSENT 客戶端已被建立,但 open() 方法尚未被呼叫。
1 OPENED open() 方法已被呼叫。
2 HEADERS_RECEIVED send() 方法已被呼叫,而且可取得 header 與狀態。
3 LOADING 回應資料下載中,此時 responseText 會擁有部分資料。
4 DONE 完成下載操作。
UNSENT
The XMLHttpRequest client has been created, but the open() method hasn't been called yet.
OPENED
open() method has been invoked. During this state, the request headers can be set using the setRequestHeader() method and the send() method can be called which will initiate the fetch.
HEADERS_RECEIVED
send() has been called and the response headers have been received.
LOADING
Response's body is being received. If responseType is "text" or empty string, responseText will have the partial text response as it loads.
DONE
The fetch operation is complete. This could mean that either the data transfer has been completed successfully or failed.

The state names are different in Internet Explorer. Instead of UNSENT, OPENED, HEADERS_RECEIVED, LOADING and DONE, the names READYSTATE_UNINITIALIZED (0), READYSTATE_LOADING (1), READYSTATE_LOADED (2), READYSTATE_INTERACTIVE (3) and READYSTATE_COMPLETE (4) are used.

範例

var xhr = new XMLHttpRequest();
console.log('UNSENT', xhr.readyState); // readyState will be 0

xhr.open('GET', '/api', true);
console.log('OPENED', xhr.readyState); // readyState will be 1

xhr.onprogress = function () {
    console.log('LOADING', xhr.readyState); // readyState will be 3
};

xhr.onload = function () {
    console.log('DONE', xhr.readyState); // readyState will be 4
};

xhr.send(null);

規範

Specification Status Comment
XMLHttpRequest Living Standard WHATWG living standard

瀏覽器相容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1 1.0 (1.7 or earlier)[1] 7 (Yes) 1.2
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? 1.0 (Yes) ? ? ?

文件標籤與貢獻者

 此頁面的貢獻者: jackblackevo
 最近更新: jackblackevo,