简体   繁体   English

jQuery AJAX 回调

[英]jQuery AJAX callback

I'm having a difficult time trying to get this javascript function to return false inside of a.post() function.我很难让这个 javascript function 在 a.post() function 内部返回 false。

Is this even possible?这甚至可能吗? or is there another way to do a simple ajax check to validate the voucher code is in the database.或者是否有另一种方法可以进行简单的 ajax 检查以验证凭证代码是否在数据库中。

function check_options(){       
        var voucher_code = $('#voucher_code').val();
        $.post(baseURL+"ajax.php", { tool: "vouchers", action: "check_voucher", voucher_code: voucher_code },
            function(data) {
            if(data == 'invalid'){
                // NEED TO RETURN FALSE FOR THE MAIN FUNCTION
            }
        });

}

You can't return false for the main function because it's already processed by the time the ajax call completes.您不能为主 function 返回 false,因为在 ajax 调用完成时它已经被处理。 You'll need to use callbacks.您需要使用回调。

function check_options(callback) {
    var voucher_code = $('#voucher_code').val();
    $.post(baseURL + "ajax.php", {
        tool: "vouchers",
        action: "check_voucher",
        voucher_code: voucher_code
    }, function(data) {
        if (data == 'invalid') {
            callback && callback(false);
        } else {
            callback && callback(true);
        }
    });

}

check_options(function(result) {
    // Handle True/False based on result
});

You can't return false for the main function, because the post call is asynchronous.您不能为主 function 返回 false,因为 post 调用是异步的。 The function will return immediately after the $.post call, now when the response arrives. function 将在 $.post 调用后立即返回,现在是响应到达时。 When the network call returns, then the function passed in the parameter will be invoked, but at that point the main function will have been long finished.当网络调用返回时,传入参数的 function 将被调用,但此时主 function 将已经完成。

Basically, you're trying to do a synchronous network call, and there is no way to do that in JS.基本上,您正在尝试进行同步网络调用,而在 JS 中没有办法做到这一点。

function check_options(){       
  var voucher_code = $('#voucher_code').val();
  $.ajax({
    type: 'POST',,
    url: baseURL+"ajax.php",
    data: { tool: "vouchers", action: "check_voucher", voucher_code: voucher_code },
    success: function(msg){
      alert('Success!');
    },
    error:function(e){
      alert('Fail!');
    }
  });
}

The $.post is asynchronous as mentioned.如前所述, $.post是异步的。 As such, the function where you want to return false is actually executing after the main function ( check_options ) completes execution (and returns).因此,您想要返回 false 的 function 实际上是在主 function ( check_options )完成执行(并返回)之后执行的。 I would suggest modifying your flow of control to account for an asynchronous call, or make the call synchronous (there should be an additional param, "async" I believe).我建议修改您的控制流程以考虑异步调用,或使调用同步(应该有一个额外的参数,我相信“异步”)。

$.post({async:false, ...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM