ajax readyState的五種狀態詳解
2024-09-01 08:29:31
供稿:網友
在《Pragmatic Ajax A Web 2.0 Primer 》中對readyStae狀態的介紹,摘譯如下:
0: (Uninitialized) the send( ) method has not yet been invoked.
1: (Loading) the send( ) method has been invoked, request in progress.
2: (Loaded) the send( ) method has completed, entire response received.
3: (Interactive) the response is being parsed.
4: (Completed) the response has been parsed, is ready for harvesting.
0 - (未初始化)還沒有調用send()方法
1 - (載入)已調用send()方法,正在發送請求
2 - (載入完成)send()方法執行完成,已經接收到全部響應內容
3 - (交互)正在解析響應內容
4 - (完成)響應內容解析完成,可以在客戶端調用了
對于readyState的這五種狀態,其他書中大都語焉不詳。像《Foundations of Ajax》中,只在書中的表2-2簡單地列舉了狀態的“名稱”--The state of the request. The five possible values are 0 = uninitialized, 1 = loading, 2 = loaded, 3 = interactive, and 4 = complete。而《Ajax in Action》中好像根本就沒有提到這5種狀態的細節。
《Professional Ajax》中雖不盡人意,但還是有可取之處:
There are five possible values for readyState:
0 (Uninitialized): The object has been created but the open() method hasn't been called.
1 (Loading): The open() method has been called but the request hasn't been sent.
2 (Loaded): The request has been sent.
3 (Interactive). A partial response has been received.
4 (Complete): All data has been received and the connection has been closed.
readyState有五種可能的值:
0 (未初始化): (XMLHttpRequest)對象已經創建,但還沒有調用open()方法。
1 (載入):已經調用open() 方法,但尚未發送請求。
2 (載入完成): 請求已經發送完成。
3 (交互):可以接收到部分響應數據。
4 (完成):已經接收到了全部數據,并且連接已經關閉。
在《Understanding AJAX: Using JavaScript to Create Rich Internet Applications》中,則用下表進行了說明:
readyState Status Code
Status of the XMLHttpRequest Object
(0) UNINITIALIZED 未初始化
The object has been created but not initialized. (The open method has not been called.)
(XMLHttpRequest)對象已經創建,但尚未初始化(還沒有調用open方法)。
(1) LOADING 載入
The object has been created, but the send method has not been called.
(XMLHttpRequest)對象已經創建,但尚未調用send方法。
(2) LOADED 載入完成
The send method has been called, but the status and headers are not yet available.
已經調用send方法,(HTTP響應)狀態及頭部還不可用。
(3) INTERACTIVE 交互
Some data has been received. Calling the responseBody and responseText properties at this state to obtain partial results will return an error, because status and response headers are not fully available.
已經接收部分數據。但若在此時調用responseBody和responseText屬性獲取部分結果將會產生錯誤,因為狀態和響應頭部還不完全可用。