ajax回调window.location请求已中止

时间:2018-03-24 01:35:43

标签: javascript jquery ajax window.location

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

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

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

  

RequestAbortedError:请求已中止

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

enter image description here

" 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的Client-Sessions npm包

服务器上的日志正在记录" Passed1"和#34; Passed2"。它正在发送客户端"无效"基于get电话中的状态。

1 个答案:

答案 0 :(得分:0)

基于进一步阅读有关快递和我在这个问题上收到的一些评论,我决定重新考虑我的方法并寻找一个更好的选择,我很高兴地说我在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();
    }
});
相关问题