Five state descriptions of readystate in AJAX applications

Source: Internet
Author: User
Tags format object prepare client

In the "Pragmatic Ajax (Dynamic Web site static) A Web 2.0 Primer" accidentally see the introduction of Readystae status, feeling this introduction is very real, selected passage as follows:

0: (uninitialized) the Send () method has not yet been invoked.
1: (Loading) the Send () method has been invoked and 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
1-(load) called Send () method, sending request
2-(load complete) Send () method completed, received all response content
3-(interactive) parsing response content
4-(complete) The response content resolution is complete and can be invoked on the client

For these five states of readystate, most of the other books are vague. Like the foundations of Ajax (Dynamic Web site statics), only table 2-2 in the book simply lists the state's "name"--the the request. The five possible values are 0 = uninitialized, 1 = loading, 2 = loaded, 3 = interactive, and 4 = complete. The Ajax (Dynamic Web site static) in action does not seem to mention the details of these 5 states at all. "Professional Ajax" (Dynamic Web site static), although not satisfactory, but there are some merits:

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.

There are five possible values for readystate:
0 (Uninitialized): (XML (more recently) HttpRequest) object has been created, but the open () method has not yet been invoked.
1 (load): The Open () method has been called, but the request has not been sent.
2 (Load complete): The request has been sent to complete.
3 (Interactive): Can receive partial response data.
4 (complete): All data has been received and the connection has been closed.

In the understanding Ajax (Dynamic Web site static): Using JavaScript to Create Rich Internet applications, this is described in the following table:

ReadyState Status Code
Status of the XML (standardization is getting closer) HttpRequest Object
(0) uninitialized
The object has been created but not initialized was not initialized. (The Open method has not been called.)
(XML (standardization is getting closer) the HttpRequest object has been created, but has not yet been initialized (the open method has not yet been called).
(1) LOADING
Load the object has been created, but the Send method to has not been called.
(XML (standardization is getting closer) HttpRequest) object has been created, but the Send method has not been invoked.
(2) LOADED
Loading completes the Send method has been called, but the status and headers are not yet.
The Send method has been called (HTTP response) status and the head is not available yet.
(3) INTERACTIVE
Interactive Some data has been received. Calling the Responsebody and ResponseText properties at this state to obtain partial results'll return the error, because Status and response headers are not fully available.
Partial data has been received. However, invoking the Responsebody and ResponseText properties at this point will result in an error because the state and response headers are not fully available.
(4) COMPLETED
Completes all the data has been received, and the complete data are available in the Responsebody and ResponseText properties.
All data has been received, and the full data can be extracted in the Responsebody and ResponseText properties.

based on the above several books on the readystate five states of the introduction, I think it is "pragmatic Ajax (Dynamic Web site static) a Web 2.0 Primer" relatively in place, Because it mentions the parsing problem of the data received, this is not mentioned in any other book, and this is the reason why the "(3) interaction" phase exists between "(2) Load Completion" and "(4) Completion" as a necessary conversion process, that is, what the task is. To sum up, I think the more ideal interpretation method should be "State: Task (target) + process + performance (or feature)" expression mode to define these states more accurate, and easy to understand. The summary is as follows:

ReadyState State
Status description

(0) not initialized
This phase confirms that the XML (standardization is getting closer) HttpRequest the object is created and ready for an uninitialized turn to invoke the open () method. A value of 0 indicates that the object already exists, or the browser will complain--the object does not exist.

(1) Loading
This phase initializes the HttpRequest object for XML (standardization is getting closer) by calling the open () method, which completes the setting of the object state based on the parameter (method,url,true). and call the Send () method to start sending the request to the server. A value of 1 indicates that a request is being sent to the server.

(2) Loading complete
This phase receives server-side response data. However, only the original data of the service-side response is obtained and cannot be used directly from the client. A value of 2 indicates that the full response data has been received. and prepare for the next phase of data parsing.

(3) Interactive
This phase resolves the received server-side response data. This translates the data into a format that can be accessed by responsebody, ResponseText, or responsexml (normalized) attributes, based on the MIME type returned by the server-side response header, to prepare for the client invocation. State 3 indicates that data is being parsed.

(4) Complete
This phase confirms that all data has been resolved to a format that is available to the client and that parsing is complete. A value of 4 indicates that the data has been parsed and can be obtained by XML, which is becoming more and more near to the HttpRequest object's corresponding properties.

In general, the entire XML (standardization is getting closer) the lifecycle of the HttpRequest object should include the following phases:
Create-Initialize request-Send request-receive data-parse data-complete

In specific applications, it is clear that the meaning of the five states of ReadyState (XML (which is becoming more and more standardized) HttpRequest the lifecycle of an object can eliminate the mystique of the core of Ajax (Dynamic Web site statics) (behind the vague or mysterious, Create mystery, or "Ichica, Shoro"), quickly grasp its essence, to reduce learning frustration and enhance self-confidence are extremely beneficial.

For example, use the following example:


Declaring an array
var states = [initializing ...],
"Initializing request ... Success!
Sending request ... ",
Success
Receiving data ... ",
Complete
Parsing data ... ",
Complete
”];

callback function internal code fragment
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)
{
The Var XML (standardisation is getting closer) Doc = XML (standardization is getting closer) Http.responsexml (standardization is getting closer);
Other code
}

Don't forget to destroy, prevent memory leaks
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 request .... Success!
Sending request ... Success!
Receiving data ... Complete!
Parsing data ... Complete!

It's easy to see how XML (Standardization is getting closer) what HttpRequest objects do at all stages. So it's easy to have a really straightforward understanding of the core parts of Ajax (Dynamic Web site statics).



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.