Ajax synchronization, Asynchronization, and instance demonstration

Source: Internet
Author: User

There are two forms of submission in Ajax: synchronous and asynchronous.
XMLHTTP. Open ("get", URL, true );

XMLHTTP. Open ("get", URL, false );

Synchronization: after submission, wait for the server to respond. After receiving the data returned by the server, run the following code:
Asynchronous: In contrast to the above, after submission, the following code continues to be executed, while listening continues in the background. After the server responds, the program processes the Code accordingly, the advantage of asynchronous operations is that you can continue to do other things on the client without waiting for the server.

Example:

Function dbonclick (){

VaR req
=
New
XMLHttpRequest ();


VaR url = "www.google.cn ";



Req. Open (
"
Get
"
, URL,
True
);
//
Establish a connection with the server (Request Method post or get, URL, true indicates asynchronous, false indicates synchronous)





Req. onreadystatechange
=
Callback;
//
Specify callback function


Req. Send (
Null
);
//
Send request


Alert ("asynchronous mode"); // if it is an asynchronous request, this sentence



Directly execute the preceding sentence,



The server does not need to wait for the server to return the response before execution. The server response is monitored by the callback function.


// For Synchronous requests, this sentence can be executed only when the server returns a response


}


 

//
Callback function, which processes responses to the server and monitors the response status.

Function callback (){



If
(Req. readystate
=
4
)
//
If the request status is 4, the request is successful.


{

If
(Req. Status
=
200
)
//
HTTP status 200 indicates OK


{
Dosomething;
//
All statuses are successful, and the corresponding code is executed.



Alert (req.




Responsetext
);

} Else {








Alert (
"
Server return status
"
 
+
Req. statustext );



}



} Else {



Alert ("loading data ...");

}




}


 

XMLHTTPRequest object methods and attributes

Fang

Method

Description

Description

Abort ()

Stop current request

GetAllResponseHeaders ()

Set
HTTP
All Response Headers in the request are used as keys.
/
Return Value Pair

GetResponseHeader ("Header ")

Returns the string value of the specified header.

Open ("method", "url ")

Create a call to the server.
Method
The parameter can be
Get
,
Post
Or
Put
.
URL
The parameter can be relative
URL
Or absolute
URL
. This method also includes
3
Optional parameters

Send (content)

Send a request to the server

SetRequestHeader ("Header", "value ")

Set the specified header to the provided value. You must call
Open ()

 

Owner
Sex

Description

Description

Onreadystatechange

This event processor is triggered when each state changes, usually calling
Javascript
Function

Readystate

Request status. Yes
5
Optional values:
0 =
Not initialized,
1 =
Loading,
2 =
Loaded,
3 =
Interacting,
4 =
Complete

Responsetext

Server Response, represented as a string

Responsexml

The response of the server, expressed
XML
. This object can be parsed into
Dom
Object

Status

Server
HTTP
Status Code (
200
Corresponding
OK
,
404
Corresponding
Not found
(Not found), and so on)

Statustext

HTTP
The corresponding text of the status code (
OK
Or
Not found
(Not found) and so on)





 





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.