【问题标题】:How to make a function wait for jquery ajax to complete如何使函数等待jquery ajax完成
【发布时间】:2014-03-19 23:10:33
【问题描述】:

我有一个登录表单,当用户单击提交按钮时,我有一个主函数,我调用它来决定页面是否应该迁移。在 main 函数中,我调用了另外两个函数:一个检查用户名和密码字段是否为空,第二个函数有一个 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>

非常感谢您的帮助

【问题讨论】:

  • 如果要同步,请改用 .ajax,并传递 async:false。

标签: jquery ajax


【解决方案1】:

您不能从异步方法返回值,而是需要使用回调方法,如

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

【讨论】:

  • ,谢谢,我认为这可能有效,但如果您不介意,请进一步澄清:在 is_in_database() 函数中,您已检查回调(a=="isthere"),现在这个变量a,我们是否像我在我的问题中所做的那样全局设置它并且仍然在回调之前(a = =“isthere”)?或者我们如何放置它?提前致谢。
  • @user243974 抱歉...应该是feedbak == 'isthere'
  • 非常感谢您的帮助;改变有帮助
【解决方案2】:

使用成功函数

   $.ajax({

    success: function(data){

    },
    error: function(error){

    }
 });

【讨论】:

    【解决方案3】:

    您的问题在这部分:

    $.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 事件。在这种情况下,您将使用您的 api 在登录成功时返回 200(.success)或在用户名/密码错误时返回 401(未经授权)或类似的东西。这样,您就可以轻松区分成功尝试和错误尝试。并直观地通知用户正在发生的事情

    希望这会有所帮助。

    【讨论】:

    • 致 Janr:=>感谢您的贡献,但您知道 ajax 函数没有显式返回它的值,因为它不是被调用的,而且它在另一个函数中。所以它不会返回到 mymain() 函数。没有效果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2017-03-15
    • 2012-05-04
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多