繁体   English   中英

Ajax成功功能未运行

[英]Ajax success function not running

我有以下我提出的Ajax请求。 当前,如果json响应为true则运行成功回调。 但是,如果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();
                }
            });
        }
    }
});

所以我的问题是,即使响应为false ,如何使回调运行?

谢谢

也许您需要使用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();
                }
            });
        }
    }
});

您确定当用户不存在时, json.user_exists确实等于false吗? 如果我对您的理解正确,则不会触发if (json.user_exists == false){...}语句,因此那里有些奇怪。

要获得更强大的代码,您可以更改

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

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

暂无
暂无

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

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