ajax 回调 window.location 请求中止

ajax callback window.location Request Aborted

我目前正在尝试为无效用户创建客户端重新路由。服务器验证用户是否有权访问当前页面,如果没有,则在 ajax 调用的 success 回调中验证 returns {data: 'invalid'} 我使用以下内容检查此值:

if (data.status === 'invalid') {
    window.location.href = domain + '/login';
    return false;
} 

这有时有效,但有时我会收到以下浏览器警告消息:

RequestAbortedError: Request aborted

我曾尝试将 window.location.href 替换为 window.location.replace()top.location.href,但都没有解决问题。我可以看到服务器正在正确处理信息并返回 {data: 'invalid'} 但是一旦它尝试 运行 行 window.location.href 我收到此错误。如果有帮助,我在下面有一张图片。

单击 "OK" 时,页面会重定向到相应的页面。最终结果如预期发生,但我无法解决错误。

更新包括服务器端代码

function authentication (req, res, next) {
    console.log('entered');
    if (typeof req.rsaConPortal.email !== 'undefined') { // Check if session exists
        console.log('passed 1');
        User.findOne({ "user.email": req.rsaConPortal.email, "user.status”: req.resConPortal.status}, function (err, user) {
            if (!user) {
                console.log('failed 2');
                req.rsaConPortal.reset();
                res.send({status: 'invalid'});
            } else {
                console.log('passed 2');
                req.rsaConPortal.email = user.user.email;
                req.rsaConPortal.id = user._id;
                req.rsaConPortal.status = user.user.status;

                next();
            } 
        });
    } else {
        console.log('failed 1');
        res.send({status: 'invalid'});
    }
}

app.get('/api/savedApp/', authentication, function(req, res) {

    if (req.rsaConPortal.status !== 'registered') {
        res.send({status: 'invalid'});
    } else {

        User.find({ "_id": req.rsaConPortal.id }, {
            profile: 1, documents: 1 }, function(err, user) {

            if (err) throw err;

            res.send(user);
        });  
    }    
});

是否有更好的方法来验证我的用户?我正在使用 Mozilla 的客户端会话 npm 包

服务器上的日志正在记录 "Passed1" 和 "Passed2"。它根据 get 调用中的状态发送客户端 "Invalid"。

基于对 express 的进一步阅读和我收到的关于这个问题的一些评论,我决定 re-think 我的方法并寻找一个更好的选择,我很高兴地说我在 express.Router。我能够创建一个身份验证功能来确定用户是否被授权,并处理是否让用户通过或将它们发送回登录的业务逻辑。然后我为我拥有的每个页面创建了一个路由,它根据用户状态进一步处理业务逻辑,让他们通过或将他们发送回登录。

感谢所有调查此问题并提供意见的人。

var router = express.Router();
app.use(router);

var authenticate = function(req, res, next) {
    if (req.rsaConPortal !== undefined) {
        if (req.rsaConPortal.email !== undefined) { // Check if session exists
            // lookup the user in the DB by pulling their email from the session
            User.findOne({ "user.email": req.rsaConPortal.email, "user.password": req.rsaConPortal.passport }, function (err, user) {
                if (!user) {
                    // if the user isn't found in the DB, reset the session info and
                    // redirect the user to the login page
                    req.rsaConPortal.reset();
                    req.rsaConPortal.email = '';
                    req.rsaConPortal.passport = '';
                    req.rsaConPortal.id = '';
                    req.rsaConPortal.status = '';

                    res.redirect('../login');
                } else {
                    req.rsaConPortal.email = user.user.email;
                    req.rsaConPortal.passport = user.user.password;
                    req.rsaConPortal.id = user._id + '';
                    req.rsaConPortal.status = user.user.status;

                    next();
                } 
            });
        } else {
            res.redirect('../login');
        }
    } else {
        res.redirect('../login');
    }
};

router.get(['/','/create_warranty','/help','/marketing_assets','my_documents','profile'], authenticate, function(req, res, next) {
    if (req.rsaConPortal.status !== 'approved') {
        res.redirect('../login');
    } else {
        next();
    }
});