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) |