About the five states of XMLHttpRequest's readystate properties yesterday did a use of Ajax to achieve a page without refreshing from the server to obtain the time of the example, then for the XMLHttpRequest object readystate the last three states is not very clear, Thought for a long time, also do not understand. Later, in different parts of the program, use Alert (xmlhttp.readystate) to view changes in the value of ReadyState, and after consulting a friend, To figure out a few of these problems: Before you create a XMLHttpRequest object, the readystate value is 0 before you call Open (), and then the Open () becomes 1, and then the onReadyStateChange function and open () executed almost simultaneously. After calling the Send method, the readystate value in the Starthttprequest function is still 1, and after the Send method is called, there should be 2,3,4 three states. Only 3 values can be observed in the Starthttprequest function with the alert statement. Why is that? This is because in the Starthttprequest function when parsing to send this sentence, does not really start to execute send execution. Only send executes, you can observe the change of state value in the onReadyStateChange function. ReadyState is not the state of sending, it is ready to send the state, to think of it as "human cannon level preparation, level two preparation, put" such slogans, not the request to send itself. At the same time, XMLHTTP is not listening to the server information, it is in send time to get the server returned state information, only once, monitoring is always in the observation state. At this point, the hearts of doubt all untied.
A summary of the five states of ReadyState is as follows:
ReadyState Status Status Description
(0) not initialized
This phase confirms that the XMLHttpRequest object is created and is 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 XMLHttpRequest object by calling the open () method, which finishes setting 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. That is, the data is converted to a format that can be accessed through the responsebody, ResponseText, or Responsexml properties 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 XMLHttpRequest the corresponding property of the object.
In general, the lifecycle of the entire XMLHttpRequest object should include the following phases:
Create-Initialize request-Send request-receive data-parse data-complete
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