从jQuery的AJAX调用返回 [英] return from jquery ajax call
问题描述
喜IM试着使用来自于我自己的函数一个jQuery AJAX调用返回,但它一直返回undefined。
hi im tryin to use the return from a jquery ajax call in my own function, but it keeps returning undefined.
function checkUser2(asdf) {
$.ajax({
type: "POST",
async: false,
url: "check_user.php",
data: { name: asdf },
success: function(data){
return data;
//alert(data);
}
});
}
$("#check").click(function(){
alert(checkUser2("muma"));
});
在Ajax调用肯定的作品,因为当我取消我的提醒,我得到正确的回报,我可以看到它在萤火虫。我在做一些愚蠢的事。
the ajax call definately works, because when i uncomment my alert i get the correct return, and i can see it in firebug. Am i doing something stupid.
推荐答案
Ajax调用是异步的 - 这意味着AJAX请求是乱序通常的程序执行,并在你的计划,意味着 checkUser2()
不将数据返回到警报。
The AJAX call is asynchronous - this means that the AJAX request is made out-of-order of usual program execution, and in your program that means that checkUser2()
is not returning data to the alert.
您无法从 $使用的返回值。阿贾克斯()
调用此方法,而不是将code,它利用AJAX返回数据的成功()
的功能 - 这就是它的功能
You cannot use return values from $.ajax()
calls in this way, instead move the code that utilises the AJAX return data to the success()
function - that what it's for.
这篇关于从jQuery的AJAX调用返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!