Ajax synchronization and asynchronous differences, first look at 2 pieces of code:
Code One:
Copy Code code as follows:
Synchronize = function (Url,param) {
function Createxhrobject () {
var http;
var ActiveX = [MSXML2. xmlhttp.3.0 "," MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
try {
http = new XMLHttpRequest;
catch (e) {
for (var i = 0; i < activex.length; ++i) {
try {
http = new ActiveXObject (Activex[i]);
Break
catch (e) {}
}
finally {
return HTTP;
}
}
var conn = Createxhrobject ();
Conn.Open ("POST", url, false);//ajax Sync
Conn.send (param);
var strreturn = Conn.responsetext;
Alert ("1");
if (Strreturn!= "") {
Return Ext.decode (Conn.responsetext);
} else {
return null;
}
Alert ("2");
};
Code two:
Ajax Synchronous Request Method:
Copy Code code as follows:
Synchronize = function (Url,param) {
function Createxhrobject () {
var http;
var ActiveX = [MSXML2. xmlhttp.3.0 "," MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
try {
http = new XMLHttpRequest;
catch (e) {
for (var i = 0; i < activex.length; ++i) {
try {
http = new ActiveXObject (Activex[i]);
Break
catch (e) {}
}
finally {
return HTTP;
}
}
var conn = Createxhrobject ();
Conn.Open ("POST", url, True);//ajax asynchronous
Conn.send (param);
var strreturn = Conn.responsetext;
Alert ("1");
if (Strreturn!= "") {
Return Ext.decode (Conn.responsetext);
} else {
return null;
}
Alert ("2");
};
The differences between synchronization and Asynchrony are as follows:
Conn.Open (' POST ', url,true); Ajax asynchronous
Conn.Open (' POST ', url,false); Ajax Sync
For code two, the result of the asynchronous AJAX request is: Execute alert (2) and then execute alert (1), which means that once the Conn.Open request is sent, the front end does not wait for its response to execute the following code, so alert (2) executes it first, Then alert (1) is executed when the response response arrives;
For code one, for synchronous AJAX requests, the execution result is: First execute alert (1) and then execute alert (2), which means that once the Conn.Open request is sent, the front end waits for its response, and after the response completes, alert (1) executes first and then alert ( 2);