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);
}