简体   繁体   English

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

[英]ajax callback window.location Request Aborted

I am currently trying to create a client-side reroute for users that are invalid. 我目前正在尝试为无效的用户创建客户端重新路由。 The server validates if the user has access to the current page and if not it returns {data: 'invalid'} in the success callback of the ajax call I check this value with the following: 服务器验证用户是否有权访问当前页面,如果没有访问权限,则它将在ajax调用的success回调中返回{data: 'invalid'} ,我使用以下命令检查该值:

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

This works sometimes but other times I receive the following browser alert message: 有时可以使用,但有时我会收到以下浏览器警报消息:

RequestAbortedError: Request aborted RequestAbortedError:请求中止

I have attempted to swap out window.location.href with window.location.replace() and top.location.href but neither resolved the issue. 我试图用window.location.replace()top.location.href换出window.location.href ,但都没有解决问题。 I can see that the server is processing the information correctly and returning {data: 'invalid'} but as soon as it tries to run the line window.location.href I receive this error. 我可以看到服务器正在正确处理信息并返回{data: 'invalid'}但是一旦它尝试运行window.location.href行,我就会收到此错误。 I have an image below if it helps. 如果有帮助,我在下面有一张图片。

在此处输入图片说明

When "OK" is clicked the page does redirect to the appropriate page. 单击“确定”后,页面确实重定向到适当的页面。 The end result is happening as expected but I cannot resolve the error. 最终结果按预期发生,但我无法解决该错误。

UPDATE INCLUDING SERVER SIDE CODE 更新,包括服务器端代码

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);
        });  
    }    
});

Is there a better way to authenticate my users? 有没有更好的方法来验证我的用户? I am using Mozilla's Client-Sessions npm package 我正在使用Mozilla的Client-Sessions npm软件包

The logs on the server are logging "Passed1" and "Passed2". 服务器上的日志正在记录“ Passed1”和“ Passed2”。 It is sending the client "Invalid" based off the status inside the get call. 它基于get调用中的状态向客户端发送“无效”消息。

Based on reading further about express and a few comments I have received on this question I have decided to re-think my approach and look for a better alternative which I am happy to say I have found in express.Router. 基于对Express的进一步阅读和对这个问题的一些评论,我决定重新考虑我的方法,并寻找一个更好的替代方法,我很高兴地说我在express.Router中找到了。 I was able to create an authentication function to determine if the user is authorized and handle the business logic of whether to let the user pass or send them back to the login. 我能够创建一个身份验证功能来确定用户是否被授权,并处理是否允许用户通过或将其发送回登录的业务逻辑。 Then I created a route for each page that I have that takes the business logic a step further based on the users status and either lets them pass or sends them back to login. 然后,我为我拥有的每个页面创建了一条路由,该路由根据用户的状态使业务逻辑更进一步,并允许他们传递或发送回登录。

Thanks to everyone who looked into this and provided comments. 感谢所有调查此事并提供评论的人。

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