Jqueryajax synchronous asynchronous execution example code _ jquery

Source: Internet
Author: User
The sample code for Synchronous asynchronous execution of jqueryajax can be found by friends who need it. Let's take a look at a simple piece of JavaScript code returned by jquery ajax.
Code

The Code is as follows:


Function getReturnAjax {
$. Ajax ({
Type: "POST ",
Http://www.jb51.net/userexist.aspx ",
Data: "username =" + vusername. value,
Success: function (msg ){
If (msg = "OK "){
Showtipex (vusername. id ,"This user name can be used", False)
Return true;
}
Else
{
Showtipex (vusername. id ,"This user has been registered", False );
Vusername. className = "bigwrong ";
Return false;
}
}
});
}


However, when we call this getReturnAjax () method, we find that all the obtained results are false, that is, return true and return false do not work at all. Using firebug debugging in Firefox also proves that, the Code does not execute the return part at all.

If we want to define a variable in the function, assign values in ajax, and return the variable at the end of the function, will it work? Modify the Code as follows:
Code

The Code is as follows:


Function getAjaxReturn ()
{
Var bol = false;
$. Ajax ({
Type: "POST ",
Http://www.jb51.net/userexist.aspx ",
Data: "username =" + vusername. value,
Success: function (msg ){
If (msg = "OK "){
Showtipex (vusername. id ,"This user name can be used", False)
// Return true;
Bol = true;
}
Else
{
Showtipex (vusername. id ,"This user has been registered", False );
Vusername. className = "bigwrong ";
// Return false;
}
}
});
Return bol;
}


The result still does not work. The final solution is as follows:

1. Add async: false. That is, it is changed to synchronous. What does it mean? (According to my colleague, this is to execute the following js only after ajax has the returned value. In other words, the assignment in many ajax calls does not work ). In this way, the following js part is executed only after ajax assigns a value to the bol. If it was just asynchronous, it was no longer time to assign values, and it was already returned.


Code

The Code is as follows:


Function getAjaxReturn ()
{
Var bol = false;
$. Ajax ({
Type: "POST ",
Async: false,
Http://www.jb51.net/userexist.aspx ",
Data: "username =" + vusername. value,
Success: function (msg ){
If (msg = "OK "){
Showtipex (vusername. id ,"This user name can be used", False)
// Return true;
Bol = true;
}
Else
{
Showtipex (vusername. id ,"This user has been registered", False );
Vusername. className = "bigwrong ";
// Return false;
}
}
});
Return bol;
}


2. pass in a function to solve this problem.

Code

The Code is as follows:


Function getAjaxReturn (success_function, fail_function)
{
Var bol = false;
$. Ajax ({
Type: "POST ",
Http://www.jb51.net/userexist.aspx ",
Data: "username =" + vusername. value,
Success: function (msg ){
If (msg = "OK "){
Showtipex (vusername. id ,"This user name can be used", False)
Success_function (msg );
}
Else
{
Showtipex (vusername. id ,"This user has been registered", False );
Vusername. className = "bigwrong ";
Fail_function (msg );
// Return false;
}
}
});
Function success_function (info)
{
// Do what you want do
Alert (info );
}
Funciont fail_function (info)
{
// Do what you want do
Alert (info );
}

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.