繁体   English   中英

ajax回调window.location请求异常终止

[英]ajax callback window.location Request Aborted

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

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

有时可以使用,但有时我会收到以下浏览器警报消息:

RequestAbortedError:请求中止

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

在此处输入图片说明

单击“确定”后,页面确实重定向到适当的页面。 最终结果按预期发生,但我无法解决该错误。

更新,包括服务器端代码

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”和“ Passed2”。 它基于get调用中的状态向客户端发送“无效”消息。

基于对Express的进一步阅读和对这个问题的一些评论,我决定重新考虑我的方法,并寻找一个更好的替代方法,我很高兴地说我在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();
    }
});

暂无
暂无

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

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