For example: Ordinary B/s mode (synchronous) Ajax Technology (asynchronous)
Synchronization: Submit request, wait for server processing, processing completed returns this period the client browser cannot do anything
Asynchronous: Requests are processed through event triggering server processing (which is still something the browser can do)
--------------------------------------------------------------------------------------------------------------- -----
Synchronization is when you ask me to eat, I hear you go to dinner, if not heard, you keep barking until I tell you to hear, to go to dinner together.
Async is you call me, and then go to dinner, I can get the news immediately, may wait until after work to eat.
So, if I ask you to eat in a synchronized way, ask me to eat with the Async method, so you can save money.
--------------------------------------------------------------------------------------------------------------- -----
For example, a synchronous message is asynchronous when you call
Take a look at several parameters of the Open method.
. Open (Http-method, url, async, UserID, password)
(followed by the account and password, in the HTTP page that prohibits anonymous access, requires a user name and password)
First look at the asynchronous processing mode.
Where Async is a Boolean value. In the case of asynchronous communication (true), the client does not wait for the server to respond, and if it is synchronous (false), the client waits until the server returns the message before performing other operations. We need to specify the synchronization method according to the actual needs, in some pages, may be issued multiple requests, even the organization has a planned formation of large-scale high-intensity request, the next one will overwrite the previous one, this time of course to specify the synchronization mode: Flase.
Let's look at a simple jquery ajax return value JS
Code
function getreturnajax{
$.ajax ({
Type: "POST",
Http://www.cnblogs.com/wlmemail/admin/%22ajax/userexist.aspx ",
Data: "Username=" +vusername.value,
Success:function (msg) {
if (msg== "OK") {
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ' > This user name can be used </font ></b> ", false)
return true;
}
Else
{
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ' > The user has been registered </ Font></b> ", false);
Vusername.classname= "Bigwrong";
return false;
}
}
});
}
But we call this getreturnajax () to find that always get is false, that is to say return True,return false does not work at all, under the Firefox with Firebug debugging also proves that the code will not execute to return part at all.
Let's just think of defining a variable in the function, then assigning it in Ajax, and finally returning the variable at the end of the function, will it work? Let's modify the code as follows:
Code
function Getajaxreturn ()
{
var Bol=false;
$.ajax ({
Type: "POST",
Http://www.cnblogs.com/wlmemail/admin/%22ajax/userexist.aspx ",
Data: "Username=" +vusername.value,
Success:function (msg) {
if (msg== "OK") {
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ' > This user name can be used </font ></b> ", false)
return true;
Bol=true;
}
Else
{
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ' > The user has been registered </ Font></b> ", false);
Vusername.classname= "Bigwrong";
return false;
}
}
});
return BOL;
}
The results still do not work. The Final Solution is 2, as follows
1, add Async:false. That is, modified to synchronize, what does it mean? (as explained by my colleague, this is when the Ajax has a return value before it executes the following JS.) Gave away, no wonder before many Ajax calls inside the assignment does not work). such as Ajax to Bol assignment is complete, the following JS section is executed. But just asynchronously, there is no time to assign a value, it has already returned.
Code
function Getajaxreturn ()
{
var Bol=false;
$.ajax ({
Type: "POST",
Async:false,
Http://www.cnblogs.com/wlmemail/admin/%22ajax/userexist.aspx ",
Data: "Username=" +vusername.value,
Success:function (msg) {
if (msg== "OK") {
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ' > This user name can be used </font ></b> ", false)
return true;
Bol=true;
}
Else
{
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ' > The user has been registered </ Font></b> ", false);
Vusername.classname= "Bigwrong";
return false;
}
}
});
return BOL;
}
2. Solve the problem by passing in a function.
Code
function Getajaxreturn (success_function,fail_function)
{
var Bol=false;
$.ajax ({
Type: "POST",
Http://www.cnblogs.com/wlmemail/admin/%22ajax/userexist.aspx ",
Data: "Username=" +vusername.value,
Success:function (msg) {
if (msg== "OK") {
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ' > This user name can be used </font ></b> ", false)
Success_function (msg);
}
Else
{
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ' > The user has been registered </ Font></b> ", false);
Vusername.classname= "Bigwrong";
Fail_function (msg);
return false;
}
}
});
function Success_function (info)
{
Do-what-want do
alert (info);
}
Funciont Fail_function (Info)
{
Do-what-want do
alert (info);
}
The difference between Ajax synchronization and Asynchrony