Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让函数等待jQueryAjax完成_Jquery_Ajax - Fatal编程技术网

如何让函数等待jQueryAjax完成

如何让函数等待jQueryAjax完成,jquery,ajax,Jquery,Ajax,我有一个登录表单,当用户单击submit按钮时,我有一个主函数,我调用它来决定页面是否应该迁移。在主要功能内,,我调用了另外两个函数:一个用于检查username和password字段是否为空,第二个函数有一个ajax,用于检查用户名和密码的组合是否正确:问题是,使用ajax的函数无法正常工作,因为主函数没有等待使用ajax的内部函数完成(异步): 以下是示例代码: <h3 id="myerror" style="display:none;">Invalid username or

我有一个登录表单,当用户单击submit按钮时,我有一个主函数,我调用它来决定页面是否应该迁移。在主要功能内,,我调用了另外两个函数:一个用于检查username和password字段是否为空,第二个函数有一个ajax,用于检查用户名和密码的组合是否正确:问题是,使用ajax的函数无法正常工作,因为主函数没有等待使用ajax的内部函数完成(异步): 以下是示例代码:

<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")
    });
}

如果需要同步,请改用.ajax,并将async:false.To Janr:=>传递给Janr:=>感谢您的贡献,但您知道ajax函数不会显式返回其值,因为它不是被调用的函数,而且它在另一个函数中。因此它不会返回mymain()函数。没有效果,谢谢,我想这可能行得通,但如果您不介意的话,请再澄清一点:在is_in_database()函数中,您已经检查了回调(a==“isthere”),现在这个变量a,我们是否像我在问题中一样全局设置它,还是在回调之前(a==“isthere”)?或我们如何放置?提前感谢。@user243974抱歉…应该是
feedbak==“isthere”
非常感谢您的帮助;更改已经起到了作用