Solution to the problem that the jquery ajax return value cannot be obtained

Source: Internet
Author: User

Let's take a look at a simple piece of JavaScript code returned by jquery ajax.

Code:

Copy to ClipboardReference: [www.bkjia.com] function getReturnAjax {
$. Ajax ({
Type: "POST ",
Url: "ajax/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 '> this user has been registered </font> </B> ", 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:

Copy to ClipboardReference: [www.bkjia.com] function getAjaxReturn ()
{
Var bol = false;
$. Ajax ({
Type: "POST ",
Url: "ajax/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 '> this user has been registered </font> </B> ", 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: Copy to ClipboardReference: [www.bkjia.com] function getAjaxReturn ()
{
Var bol = false;
$. Ajax ({
Type: "POST ",
Async: false,
Url: "ajax/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 '> this user has been registered </font> </B> ", false );
Vusername. className = "bigwrong ";
// Return false;
}
}
});
Return bol;
}


2. pass in a function to solve this problem.

Code: Copy to ClipboardReference: [www.bkjia.com] function getAjaxReturn (success_function, fail_function)
{
Var bol = false;
$. Ajax ({
Type: "POST ",
Url: "ajax/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 '> this user has been registered </font> </B> ", 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 );
}

Select the desired solution as needed. Generally, a large website uses 2nd function input methods to handle page prompts when the request is successful or fails.

Related Article

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.