使用 bvalidator 检查用户名
为什么我的函数的返回值总是 false
这是我的函数:
function checkUsername(username) {
var ret=false;
$.ajax({
type: "POST",
url: "user_validate.php",
async: true,
data: "username="+username,
success: function(data){
if(data == 0){
ret = true;
}else{
ret = false;
}
},
error:function() {
alert("ERROR");
}
});
return ret;
}
var ret 没有设置值吗? 有什么问题吗..? 谢谢之前..!
Why the return value of my function always false
this is my function:
function checkUsername(username) {
var ret=false;
$.ajax({
type: "POST",
url: "user_validate.php",
async: true,
data: "username="+username,
success: function(data){
if(data == 0){
ret = true;
}else{
ret = false;
}
},
error:function() {
alert("ERROR");
}
});
return ret;
}
Is var ret doesn't set the value?
Anything wrong with it..?
thanks before..!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AJAX 中的“A”代表“异步”< /a>
The "A" in AJAX stands for "asynchronous"
由于 AJAX 调用是异步的,因此您需要在赋值之前返回 ret 的值。
使用回调方法处理结果:
用法:
Because the AJAX call is asynchronpous, so you return the value of
ret
before it's assigned.Use a callback method to handle the result:
Usage:
您对这里事物的范围有点困惑。 ajax 调用是异步的,因此当它应答时,它不再与调用它的函数有任何关系。您真正想做的是设置某种事件侦听器或(我的首选方法)回调。
另外,帮自己一个忙,使用缩短的语法 $.post(): http://api.jquery .com/jQuery.post/
You're a little confused on the scope of things here. The ajax call is asynchronous, so by the time it answers, it no longer has anything to do with the function it was called from. What you really want to do is either set up some sort of event listener or a (my preferred method) callback.
Also, do yourself a favor and use the shortened syntax $.post(): http://api.jquery.com/jQuery.post/