jquery Ajax synchronous asynchronous execution return value can not be achieved solution _jquery

Source: Internet
Author: User
Let's take a look at a simple jquery ajax return value JS

Code
Copy Code code as follows:

function getreturnajax{
$.ajax ({
Type: "POST", Data: "Username=" +vusername.value,
Success:function (msg) {
if (msg== "OK") {
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ') > The username 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 () always get false, that is to say return True,return false does not work at all, in Firefox under the Firebug debugging also proved that the code will not be executed to the return section.

What if we just try to define a variable in a function, then assign it in Ajax, and finally return the variable at the end of the function? We modified the code as follows:

Code
Copy Code code as follows:

function Getajaxreturn () {
var Bol=false;
$.ajax ({
Type: "POST", Data: "Username=" +vusername.value,
Success:function (msg) {
if (msg== "OK") {
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ') > The username 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, change to sync, what do you mean? (according to colleague explanation is, this is wait for this Ajax to return a value before will execute JS below.) Nail secret, no wonder that many of the previous Ajax calls inside the assignment does not work. such as Ajax to Bol after the assignment is completed, the following JS to perform the section. And just asynchronous words, has not had time to assign value, it has returned.

Code
Copy Code code as follows:

function Getajaxreturn () {
var Bol=false;
$.ajax ({
Type: "POST",
Async:false, data: "Username=" +vusername.value,
Success:function (msg) {
if (msg== "OK") {
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ') > The username 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, by passing in a function to solve this problem.
Copy Code code as follows:

function Getajaxreturn (success_function,fail_function) {
var Bol=false;
$.ajax ({
Type: "POST", Data: "Username=" +vusername.value,
Success:function (msg) {
if (msg== "OK") {
Showtipex (Vusername.id, "<b><font color= ' #ffff00 ') > The username 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 your want do
alert (info); Funciont fail_function (info)//do What you want do
alert (info);
}
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.