For more information about the differences between ajax synchronous requests and asynchronous requests, see. The differences between ajax synchronization and Asynchronization are as follows:
Code 1:
The Code is 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 Synchronization
Conn. send (param );
Var strReturn = conn. responseText;
Alert ("1 ");
If (strReturn! = ""){
Return Ext. decode (conn. responseText );
} Else {
Return null;
}
Alert ("2 ");
};
Code 2:
Ajax synchronous request method:
The Code is 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 Asynchronization are as follows:
Conn. open ('post', Url, true); // ajax asynchronous
Conn. open ('post', Url, false); // ajax Synchronization
For an asynchronous ajax request in Code 2, the execution result is: Execute alert (2) and then execute alert (1). asynchronous means that once conn. when an open request is sent, the front-end will execute the code after it does not wait for its response. Therefore, alert (2) executes the code first, and then executes alert (1) only after the response arrives );
For Code 1, which is a synchronous ajax request, the execution result is: Execute alert (1) and then execute alert (2). Synchronization means that once conn. when an open request is sent, the front-end waits for its response. After the response is complete, alert (1) executes the request first, and then alert (2 );