Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

There are several states of readyState in ajax

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "there are several states of readyState in ajax". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "there are several states of readyState in ajax".

For an introduction to readyStae status in "Pragmatic Ajax A Web 2.0 Primer", the excerpt 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) has called the send () method and is sending the request

2-(load completed) the send () method has been executed and all the response contents have been received.

3-(interactive) parsing response content

4-(complete) the parsing of the response content is complete and can be called on the client side.

Most of the other books are vague about these five states of readyState. Like "Foundations of Ajax", only Table 2-2 in the book simply lists the "name" of the state-- 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 five states don't seem to be mentioned at all in "Ajax in Action".

Although "Professional Ajax" is not satisfactory, it still has 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 been closed.

There are five possible values for readyState:

0 (uninitialized): (XMLHttpRequest) the object has been created, but the open () method has not been called.

1 (load): the open () method has been called, but the request has not been sent.

2 (load completed): the request has been sent.

3 (interaction): partial 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", it is explained with the following table:

ReadyState Status Code

Status of the XMLHttpRequest Object

(0) UNINITIALIZED is not initialized

The object has been created but not initialized. (The open method has not been called.)

(XMLHttpRequest) the object has been created but has not been initialized (the open method has not been called yet).

(1) LOADING loading

The object has been created, but the send method has not been called.

The (XMLHttpRequest) object has been created, but the send method has not been called.

(2) LOADED loading completed

The send method has been called, but the status and headers are not yet available.

The send method has been called, and 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.

Part of the data has been received. However, calling the responseBody and responseText properties to get some of the results at this time will result in an error because the status and response headers are not fully available.

(4) COMPLETED completion

All the data has been received, and the complete data is available in the responseBody and responseText properties.

All the 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 A Web 2.0 Primer" is more in place, because it mentions the problem of parsing the received data, which is not mentioned in other books, and this is the reason why the "(3) interaction" phase exists between "(2) loading completion" and "(4) completion" as a necessary transition process. That's what its mission is. To sum up, I think the ideal interpretation method should be the expression pattern of "state: task (goal) + process + performance (or characteristics)" to define these states more accurately and easy to understand.

The summary is as follows:

ReadyState status description

(0) uninitialized

This phase confirms that the XMLHttpRequest object is created and is ready for non-initialization by calling the open () method. A value of 0 indicates that the object already exists, otherwise the browser will report an error-the object does not exist.

(1) load

At this stage, the XMLHttpRequest object is initialized, that is, the open () method is called to complete the setting of the object state according to 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 completed

This phase receives the response data from the server. However, what is obtained is only the original data of the server response, which can not be used directly on the client side. A value of 2 indicates that all response data has been received. And prepare for the next stage of data parsing.

(3) interaction

This phase parses the received server-side response data. That is, according to the MIME type returned by the server response header, the data is converted into a format that can be accessed through responseBody, responseText or responseXML properties, which is used as a preparation for client call. Status 3 indicates that the data is being parsed.

(4) complete

This phase confirms that all the data has been parsed to the format available to the client, and the parsing is complete. A value of 4 means that the data has been parsed and can be obtained through the corresponding properties of the XMLHttpRequest object.

In a nutshell, the entire life cycle of a XMLHttpRequest object should include the following phases:

Create-initialize request-send request-receive data-parse data-complete

In the specific application, by defining the meaning of the five states of readyState (all stages of the life cycle of XMLHttpRequest objects), we can eliminate the mystery of the core of Ajax (either mystifying and creating mystery behind the ambiguity; or "making people aware of it") and quickly grasp its essence, which is extremely beneficial to reduce frustration in learning and enhance self-confidence.

For example, through the following example:

Program code

/ / declare an array

Var states = ["initializing."

"initializing request. Success! sending a request."

"success! receiving data."

"done! parsing the data."

"done!"]

/ / callback function internal code snippet

If (xmlHttp.readyState==4)

{

Var span = document.createElement ("span")

Span [XSS _ clean] = states [xmlHttp.readyState]

Document.body.appendChild (span)

If (xmlHttp.status = 200)

{

Var xmldoc = xmlHttp.responseXML

/ / other codes

}

/ / Don't forget to destroy to prevent memory leaks

XmlHttp = null

}

Else

{

Var span = document.createElement ("span")

Span [XSS _ clean] = states [xmlHttp.readyState]

Document.body.appendChild (span)

}

The results are as follows:

Initializing request... Success!

Sending a request... Success!

Receiving data... Done!

Parsing the data... Done!

It's easy to understand what the XMLHttpRequest object is doing at all stages. Therefore, it is easy to have a really simple and clear understanding of the core parts of Ajax.

Attached:

Example 1

ReadyState returns the current status of the XMLHTTP request

Syntax 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 status is represented by an integer with a length of 4. The definition is as follows:

0 (uninitialized) object has been established, but has not been initialized (open method has not been called)

1 (initialization) object has been established and the send method has not been called

2 (send data) the send method has been called, but the current state and http header are unknown

3 (data transfer) has received part of the data, because the response and http headers are incomplete, there will be an error in obtaining part of the data through responseBody and responseText

4 (complete) the data has been received, and the complete response data can be obtained through responseBody and responseText

Example 2

The following example shows how to read the ReadyState property of a RDS.DataControl object in VBScript code at run time. ReadyState is a read-only property.

To test the example, cut and paste the code between the tags in the standard HTML document, then name it "ADCapi9.asp", and the ASP script will identify the server.

Program code

RDS API Code Examples

RDS.DataControl ReadyState property

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

These are all the contents of the article "there are several states of readyState in ajax". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report