Home > Web Front-end > JS Tutorial > body text

jquery ajax同步异步的执行最终解决方案_jquery

WBOY
Release: 2016-05-16 17:35:17
Original
1076 people have browsed it

大家先看一段简单的jquery ajax 返回值的js
代码

复制代码 代码如下:

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,"jquery ajax同步异步的执行最终解决方案_jquery该用户名可以使用",false)
return true;
}
else
{
showtipex(vusername.id,"jquery ajax同步异步的执行最终解决方案_jquery该用户已被注册",false);
vusername.className="bigwrong";
return false;
}
}
});
}

但是我们调用这个getReturnAjax()发现始终取得的都是false,那就是说return true,return false根本没有起作用,在火狐下用firebug调试也证明,代码根本不会执行到return 部分。

我们试想在函数里先定义一个变量,然后在ajax里赋值,最后在函数的末尾返回这个变量,会不会有效果呢?我们把代码修改如下:
代码
复制代码 代码如下:

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,"jquery ajax同步异步的执行最终解决方案_jquery该用户名可以使用",false)
// return true;
bol=true;
}
else
{
showtipex(vusername.id,"jquery ajax同步异步的执行最终解决方案_jquery该用户已被注册",false);
vusername.className="bigwrong";
//return false;
}
}
});
return bol;
}

结果仍然不起作用。最后解决方案有2,如下
1、添加async:false.即修改为同步了,什么意思?(按同事解释就是,这是等这个ajax有了返回值后才会执行下面的js。一语道破天机,怪不得以前很多ajax调用里面的赋值都不起作用)。这样等ajax给bol赋值完毕后,才执行下面的js部分。而刚刚异步的话,还没有来得及赋值,就已经return了。
代码
复制代码 代码如下:

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,"jquery ajax同步异步的执行最终解决方案_jquery该用户名可以使用",false)
// return true;
bol=true;
}
else
{
showtipex(vusername.id,"jquery ajax同步异步的执行最终解决方案_jquery该用户已被注册",false);
vusername.className="bigwrong";
//return false;
}
}
});
return bol;
}

2、 通过传入一个函数解决这个问题。
代码
复制代码 代码如下:

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,"jquery ajax同步异步的执行最终解决方案_jquery该用户名可以使用",false)
success_function(msg);
}
else
{
showtipex(vusername.id,"jquery ajax同步异步的执行最终解决方案_jquery该用户已被注册",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);
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!