如何让函数等待jQueryAjax完成
我有一个登录表单,当用户单击submit按钮时,我有一个主函数,我调用它来决定页面是否应该迁移。在主要功能内,,我调用了另外两个函数:一个用于检查username和password字段是否为空,第二个函数有一个ajax,用于检查用户名和密码的组合是否正确:问题是,使用ajax的函数无法正常工作,因为主函数没有等待使用ajax的内部函数完成(异步): 以下是示例代码:如何让函数等待jQueryAjax完成,jquery,ajax,Jquery,Ajax,我有一个登录表单,当用户单击submit按钮时,我有一个主函数,我调用它来决定页面是否应该迁移。在主要功能内,,我调用了另外两个函数:一个用于检查username和password字段是否为空,第二个函数有一个ajax,用于检查用户名和密码的组合是否正确:问题是,使用ajax的函数无法正常工作,因为主函数没有等待使用ajax的内部函数完成(异步): 以下是示例代码: <h3 id="myerror" style="display:none;">Invalid username or
<h3 id="myerror" style="display:none;">Invalid username or password</h3>
<form action="mylogin.php" onSubmit="return mymain()">
username<input type="text" id="myuname">
password:<input type="password" id="mypass">
</form>
//and the script
<script>
function mymain() {
if (is_not_empty() && is_in_database()) {
return true;
} else {
return false;
}
}
function is_not_empty() {
var uname = $("#myuname").val();
var pass = $("#mypass").val();
if (uname == "" && pass == "") {
$("#myerror").css("display", "block");
return false;
} else {
return true;
}
}
var a;
function isthere() {
var uname = $("#myuname").val();
var pass = $("#mypass").val();
//the ajax php below returns either isthere or notthere with reference to the username
and password combination.
$.post("http://localhost/passtester/pass1.php", {
username: uname,
password: pass
}, function (feedbak) {
a = feedbak;
});
if (a == "isthere") return true;
if (a == "notthere") return false;
}
</script>
无效的用户名或密码
用户名
密码:
//剧本呢
函数mymain(){
if(is_not_empty()&&is_in_database()){
返回true;
}否则{
返回false;
}
}
函数不是空的(){
var uname=$(“#myuname”).val();
var pass=$(“#mypass”).val();
如果(uname==“”&pass==“”){
$(“#myerror”).css(“显示”、“块”);
返回false;
}否则{
返回true;
}
}
var a;
函数isthere(){
var uname=$(“#myuname”).val();
var pass=$(“#mypass”).val();
//下面的ajaxphp返回isthere或notthere,并引用用户名
和密码组合。
$.post(”http://localhost/passtester/pass1.php", {
用户名:uname,
密码:pass
},函数(feedbak){
a=feedbak;
});
如果(a==“isthere”)返回true;
如果(a==“notthere”)返回false;
}
我将非常感谢您的帮助使用成功功能
$.ajax({
success: function(data){
},
error: function(error){
}
});
您的问题在本部分中:
$.post("http://localhost/passtester/pass1.php", {username:uname,password:pass}
,function(feedbak)
{
a=feedbak;
});
if(a=="isthere")
return true;
if(a=="notthere")
return false;
}
您的条件不在回调函数的范围内,将在处理ajax请求之前被调用:
很可能是这样的:
$.post("http://localhost/passtester/pass1.php", {username:uname,password:pass}
,function(feedbak)
{
a=feedbak;
if(a=="isthere")
return true;
if(a=="notthere")
return false;
}
});
更好的方法是在jquery中使用$.ajax()
函数,而不是简写的$.post
。这样可以访问.success
和.error
事件。在这种情况下,当登录成功(.success)或401(未经授权)时,可以使用api返回200当用户名/密码错误时,也可以使用类似的方法。这样可以轻松区分成功的尝试和错误的尝试。并直观地通知用户发生了什么
希望这有帮助。您不能从异步方法返回值,而是需要使用回调方法,如
function mymain() {
if (is_not_empty()) {
is_in_database(function (isthere) {
if (isthere) {
$('form').submit();
}
})
}
return false;
}
function is_not_empty() {
var uname = $("#myuname").val();
var pass = $("#mypass").val();
if (uname == "" && pass == "") {
$("#myerror").css("display", "block");
return false;
} else {
return true;
}
}
function is_in_database(callback) {
var uname = $("#myuname").val();
var pass = $("#mypass").val();
//the ajax php below returns either isthere or notthere with reference to the username and password combination.
$.post("http://localhost/passtester/pass1.php", {
username: uname,
password: pass
}, function (feedbak) {
callback(feedbak == "isthere")
});
}
feedbak==“isthere”
非常感谢您的帮助;更改已经起到了作用