In "Pragmatic ajax (Dynamic Website Static) A Web 2.0 Primer", I accidentally saw an introduction to the state of readyStae. I feel that this introduction is very realistic. The translation is as follows:
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 - (Uninitialized) The send() method has not been called yet
1 - (Load) The send() method has been called and the request is being sent
2 - (Loading is completed) The send() method has been executed and all response content has been received
3 - (Interaction) Response content is being parsed
4 - (Complete) The response content parsing is completed and can be called on the client
Most of the other books are vague about these five states of readyState. For example, in "Foundations of ajax", the "name" of the state is simply listed in Table 2-2 in the book - The state of the request. The five possible values are 0 = uninitialized, 1 = loading, 2 = loaded, 3 = interactive, and 4 = complete. The details of these 5 states seem to be not mentioned at all in "ajax (Dynamic Website Static) in Action". Although the "Professional ajax" is not satisfactory, it still has advantages:
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.
There are five possible values for readyState:
0 (Uninitialized): (xml (standardization is getting closer) HttpRequest) object has been created, but the open() method has not been called yet.
1 (Load): The open() method has been called, but the request has not been sent yet.
2 (Loading is completed): The request has been sent.
3 (Interaction): Part of the response data can be received.
4 (Complete): All data has been received and the connection has been closed.
In "Understanding ajax: Using JavaScript to Create Rich Internet Applications", the following table is explained:
readyState Status Code
Status of the xml(standardization is getting closer)HttpRequest Object
(0) UNINITIALIZED
The object has been created but not initialized. (The open method has not been called.)
(xml (standardization is getting closer) HttpRequest) object has been created, but has not been initialized yet (the open method has not been called yet).
(1) LOADING
Load The object has been created, but the send method has not been called.
(xml (standardization is getting closer) HttpRequest) object has been created, but the send method has not been called yet.
(2) LOADED
The send method has been called, but the status and headers are not yet available.
The send method has been called, the (HTTP response) status and header are not available.
(3) INTERACTIVE
Interaction 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.
Some data has been received. However, if you call the responseBody and responseText properties at this time to obtain some results, an error will occur because the status and response headers are not fully available.
(4) COMPLETED
All the data has been received, and the complete data is available in the responseBody and responseText properties.
All data has been received and the complete data can be extracted in the responseBody and responseText properties.
According to the introduction of the five states of readyState in the above books, I think "Pragmatic ajax (dynamic website static) A Web 2.0 Primer" is more in place, because it mentions the problem of parsing received data, which is not mentioned in other books. This is the reason why the "(3) interaction" stage exists as a necessary conversion process between "(2) load completion" and "(4) completion", that is, what is its task. In summary, I think the ideal explanation method should define these states accurately and easily understandable in the expression pattern of "state: task (objective) + process + performance (or characteristics). The current trial summary is as follows:
readyState Status Description
(0) Not initialized
In this stage, confirm whether the xml (standardization is getting closer) HttpRequest object is created and prepare for calling the open() method for uninitialization. A value of 0 means that the object already exists, otherwise the browser will report an error - the object does not exist.
(1) Load
In this stage, the xml (standardization is getting closer and closer) HttpRequest object is initialized, that is, the open() method is called to complete the setting of the object state according to the parameters (method, url, true). And call the send() method to start sending a request to the server. A value of 1 means that a request is being sent to the server.
(2) Loading is completed
This stage receives the response data from the server side. But what is obtained is the original data of the server response and cannot be used directly on the client. A value of 2 means that the complete response data has been received. And prepare for the next stage of data parsing.
(3) Interaction
In this stage, the received server-side response data is parsed. That is, according to the MIME type returned by the server-side response header, the data is converted into a format that can be accessed through the responseBody, responseText or responsesexml (standardization is getting closer and closer) attributes, preparing for client calls. Status 3 means that data is being parsed.
(4) Completed
In this stage, confirm that all data has been parsed into the format available to the client and the parsing has been completed. A value of 4 means that the data has been parsed. Data can be obtained through the corresponding properties of the HttpRequest object (standardization is getting closer and closer).
In summary, the life cycle of the entire xml (standardization is getting closer and closer) HttpRequest object should include the following stages:
Create - Initialize Request - Send Request - Receive Data - Analyze Data - Complete
In specific applications, clarifying the meaning of the five states of readyState (xml (standardization is getting closer) and the various stages of the life cycle of the HttpRequest object) can eliminate the mystery of the core of ajax (static website) (behind the vague statement is either a pretentious and mysterious way to create a sense of mystery; or "making people clear with it"), quickly grasping its essence is extremely beneficial to reducing frustration in learning and enhancing self-confidence.
For example, by following the example:
The code copy is as follows:
//Declare array
var states = ["Initializing...",
“Initializing the request…successful!
Sending a request...",
"success!
Receiving data...",
"Finish!
Analyzing data...",
"Finish!
”];
//Internal code snippet of callback function
if (xml(standardization is getting closer)Http.readyState==4)
{
var span = document.createElement("span");
span.innerHTML = states[xml(standardization is getting closer)Http.readyState];
document.body.appendChild(span);
if (xml(standardization is getting closer)Http.status == 200)
{
var xml (standardization is getting closer) doc = xml (standardization is getting closer) Http.responsexml (standardization is getting closer);
//Other code
}
//Don't forget to destroy it to prevent memory leakage
xml (standardization is getting closer) Http = null;
}else{
var span = document.createElement("span");
span.innerHTML = states[xml(standardization is getting closer)Http.readyState];
document.body.appendChild(span);
}The results are as follows:
Initializing the request...successful!
Sending a request...successful!
Receiving data...completed!
Analyzing data...completed!
It's easy to understand what the xml (standardization is getting closer) HttpRequest object is doing at all stages. Therefore, it is easy to have a truly simple and clear understanding of the core part of ajax (dynamic website static).