jquery Ajax return no workaround for returned value _jquery

Source: Internet
Author: User
Let's take a look at a simple jquery ajax return value JS
Copy Code code as follows:

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 ') > 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:

Copy Code code as follows:

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 ') > 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.

Copy Code code as follows:

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 ') > 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",
URL: "Ajax/userexist.aspx",
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 your want do
alert (info);
}

Choose your own options as needed. Generally, large Web sites use the 2nd way of passing in functions to handle page prompts for success and failure situations.
Copy Code code as follows:

Add Async:false. That is, modify to sync
And so on Ajax to Bol after the assignment, only to execute the following JS section. But the asynchronous words, has not had time to assign the value, already return.
function Vcodeyes () {
var bol = false;
$.ajax (
{
Type: "Get",
URL: ".. /ajax/validationcode.ashx ",
Data: {txtvcode: $ (' #<%=txtvcode.clientid%> '). Val ()},
Async:false,
Success:function (data) {
if (data = = "0") {
$.dialog ({icon: ' Warning ', follow:document.getElementById (' Txtvcode '), Content: ' Verification code Error! '}). Time (1);
Changecaptchaimage (' imgVerify2 ', ' txtvcode ');
}
else {
Bol = true;
}
}
});
return BOL;
}
Validation does not pass is return false otherwise perform the following function
$ ('. Getvodes '). Click (function () {
if (Vcodeyes ()) {
。。。 Perform the following ...
}
});
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.