简体   繁体   English

Ajax成功功能未运行

[英]Ajax success function not running

I have the following Ajax request which I make. 我有以下我提出的Ajax请求。 Currently if the json response is true then the success callback runs. 当前,如果json响应为true则运行成功回调。 But if the json response is false then the success callback isn't run, even if the Ajax request itself is successfully made. 但是,如果json响应为false则即使Ajax请求本身已成功发出,也不会运行成功回调。

$.ajax({
    url: "http://testdomain.com/wp-admin/admin-ajax.php",
    type: "POST",
    dataType: "json",
    cache: false,
    data: {
        action: 'check_username',
        user: user_email
    },
    success: function(json) {
        if (json.user_exists == true) {
            $.ajax({
                url: "http://testdomain.com/wp-admin/admin-ajax.php",
                type: "POST",
                dataType: "json",
                cache: false,
                data: {
                    action: 'auto_login',
                    user: user_email
                },
                success: function(json) {
                    $('#aux_lightbox_overlay').fadeOut();
                }
            });
        }
        if (json.user_exists == false) {
            $.ajax({
                url: "http://testdomain.com/wp-admin/admin-ajax.php",
                type: "POST",
                dataType: "json",
                cache: false,
                data: {
                    action: 'auto_register',
                    user: user_email
                },
                success: function(json) {
                    $('#aux_lightbox_overlay').fadeOut();
                }
            });
        }
    }
});

So my question is, how do I get the callback to run even if the response is false ? 所以我的问题是,即使响应为false ,如何使回调运行?

Thanks 谢谢

Maybe you need to use complete instead success and error . 也许您需要使用complete successerror来代替。

$.ajax({
    url: "http://testdomain.com/wp-admin/admin-ajax.php",
    type: "POST",
    dataType: "json",
    cache: false,
    data: {
        action: 'check_username',
        user: user_email
    },
    complete: function(json) {
        if (json.user_exists == true) {
            $.ajax({
                url: "http://testdomain.com/wp-admin/admin-ajax.php",
                type: "POST",
                dataType: "json",
                cache: false,
                data: {
                    action: 'auto_login',
                    user: user_email
                },
                success: function(json) {
                    $('#aux_lightbox_overlay').fadeOut();
                }
            });
        }
        if (json.user_exists == false) {
            $.ajax({
                url: "http://testdomain.com/wp-admin/admin-ajax.php",
                type: "POST",
                dataType: "json",
                cache: false,
                data: {
                    action: 'auto_register',
                    user: user_email
                },
                success: function(json) {
                    $('#aux_lightbox_overlay').fadeOut();
                }
            });
        }
    }
});

Are you sure that when the user does not exist, json.user_exists is indeed equal to false ? 您确定当用户不存在时, json.user_exists确实等于false吗? If I understand you correctly, the if (json.user_exists == false){...} statement is not triggered, so there is something weird there. 如果我对您的理解正确,则不会触发if (json.user_exists == false){...}语句,因此那里有些奇怪。

For more robust code, you could change 要获得更强大的代码,您可以更改

if (json.user_exists == true) {
  // ...
}
if (json.user_exists == false){
  // ...
}

into

if (json.user_exists == true) {
  // ...
}
else {
  // ...
}

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

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