Add async: false. That is, it is changed to synchronous. What does it mean? According to my colleagues, this is the result that the following JavaScript code will be executed only when ajax has a returned value, no wonder the assignment values in many ajax calls do not work in the past. Let's take a look at the JavaScript code returned by jquery ajax.
Code
The Code is as follows:
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 ,"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.cnblogs.com/wlmemail/admin/%22ajax/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.cnblogs.com/wlmemail/admin/%22ajax/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.cnblogs.com/wlmemail/admin/%22ajax/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 );
}