Ajax readystate Five states detailed _ajax related

Source: Internet
Author: User
The Readystae status is described in the pragmatic Ajax A Web 2.0 Primer, 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. As in "Foundations of Ajax", 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 in action does not seem to mention the details of these 5 states at all.
"Professional Ajax" is unsatisfactory, but there are some 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.
There are five possible values for readystate:
0 (uninitialized): (XMLHttpRequest) object has been created, but the open () method has not been invoked yet.
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:using JavaScript to Create Rich Internet applications, the following table describes:
ReadyState Status Code
Status of the XMLHttpRequest Object
(0) Uninitialized not initialized
The object has been created but not initialized. (The Open method has not been called.)
(XMLHttpRequest) object has been created, but has not yet been initialized (the open method has not yet been called).
(1) LOADING loading
The object has been created, but the Send method has not been called.
(XMLHttpRequest) object has been created, but the Send method has not been invoked.
(2) LOADED loading complete
The Send method is 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 interaction
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 complete
All of the data has been received, and the complete data is 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.
According to the above books on the readystate five states of the introduction, I think the "pragmatic Ajax A Web 2.0 Primer" is in place, because it refers to the data received by the resolution problem, other books do not mention this, and this is exactly "(3) The interaction phase as a necessary conversion process exists between "(2) Load Completion" to "(4) Completion", which 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.
Summarized 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
In the specific application, it is clear that the meaning of the five states of readystate (the various stages of the life cycle of the XMLHttpRequest object) can eliminate the mystery of the Ajax core (behind vague or mysterious, creating mystery; or "Ichica, Shoro"). , quickly grasp its essence, to reduce frustration in learning and enhance self-confidence are extremely beneficial.
For example, use the following example:
Program code
Declaring an array
var states = [initializing ...],
"Initializing request ... Success! <br/> sending requests ... ",
Success <br/> receiving data ... ",
Complete <br/> parsing data ... ",
Complete <br/> "];
callback function internal code fragment
if (xmlhttp.readystate==4)
{
var span = document.createelement ("span");
span.innerhtml = States[xmlhttp.readystate];
Document.body.appendChild (span);
if (Xmlhttp.status = 200)
{
var xmldoc = Xmlhttp.responsexml;
Other code
}
Don't forget to destroy, prevent memory leaks
XmlHttp = null;
}
Else
{
var span = document.createelement ("span");
span.innerhtml = States[xmlhttp.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 what the XMLHttpRequest object does at all stages. So it's easy to have a really straightforward understanding of the core parts of Ajax.
Report:
Example 1
ReadyState returns the current state of the XMLHTTP request
Grammar lValue = oxmlhttprequest.readystate;
Program code
var XmlHttp;
XmlHttp = new ActiveXObject ("msxml2.xmlhttp.3.0");
function Send () {
Xmlhttp.onreadystatechange = Dohttpreadystatechange;
Xmlhttp.open ("Get", "Http://localhost/sample.xml", true);
Xmlhttp.send ();
}
function Dohttpreadystatechange () {
if (xmlhttp.readystate = = 4) {
Alert ("Done");
}
}
Note variable, this property is read-only and the state is represented by an integer of length 4. The definition is as follows:
0 (uninitialized) object established, but not initialized (the open method has not been called)
1 (initialization) object established, send method has not been called
2 (send data) The Send method has been invoked, but the current state and HTTP headers are unknown
3 (data transfer) has received part of the data, because the response and HTTP headers are incomplete, then get some data through Responsebody and ResponseText error,
4 (complete) data received, at this time through Responsebody and responsetext to obtain complete response data
Example 2
The following example shows how to read RDS in VBScript code at run time. The ReadyState property of the DataControl object. ReadyState is a read-only property.
To test the example, cut the code and paste it between the <Body></Body> tags of the standard HTML document, and then name it "Adcapi9.asp", and the ASP script will identify the server.
Program code
<Center>
<HR>
<H3> RDS. DataControl ReadyState property</Center>
<!--RDS that sets the parameters at run time. DataControl-->
<object classid= "Clsid:bd96c556-65a3-11d0-983a-00c04fc29e33"
Id=adc>
<param name= "SQL" value= "select * from Employee for browse" >
<param name= "SERVER" value= "http://<%=Request.ServerVariables (" SERVER_NAME ")%>" >
<param name= "CONNECT" value= "DSN=ADCDEMO; Uid=adcdemo; Pwd=adcdemo; " >
<param name= "ExecuteOptions" value= "Adcexecasync" >
<param name= "FetchOptions" value= "Adcfetchasync" >
</OBJECT>
<script language= "VBScript" >
Sub Window_onload
Select case ADC1. ReadyState
Case 2:msgbox "Executing Query"
Case 3:msgbox "fetching Records in Background"
Case 4:msgbox "All records fetched"
End Select
End Sub
</Script>
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.