javascript - Ajax 成功函数未运行

标签 javascript jquery ajax json wordpress

我提出了以下 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,如何让回调运行?

谢谢

最佳答案

也许您需要使用完整而不是成功错误

$.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();
                }
            });
        }
    }
});

关于javascript - Ajax 成功函数未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25113385/

相关文章:

javascript - 如何在JS函数中动态设置键值?

javascript - Async/Await 似乎在类方法和函数表达式中有不同的行为(React Hooks,Redux-Thunk)

jquery - 如何使用 Jquery 使 future 的元素可拖动

javascript - 如何将 "crossorigin"标记添加到动态加载的脚本中?

php - 使用 Ajax 和 PHP 检查数据库中的现有用户

javascript - 使用 JavaScript 获取日期范围内的特定日期

javascript - 全栈应用的 server.js 中符号🌎 的含义

javascript - 将文件拖过区域时更改拖放区域颜色

javascript验证码不验证

php - 如何在没有 Flash 支持的情况下使用 Ajax 和 Codeigniter 上传文件?