Javascript ajax回调窗口。位置请求中止
我当前正在尝试为无效用户创建客户端重新路由。服务器验证用户是否有权访问当前页面,如果没有,则在ajax调用的Javascript ajax回调窗口。位置请求中止,javascript,jquery,ajax,window.location,Javascript,Jquery,Ajax,Window.location,我当前正在尝试为无效用户创建客户端重新路由。服务器验证用户是否有权访问当前页面,如果没有,则在ajax调用的success回调中返回{data:'invalid'},我使用以下内容检查此值: if (data.status === 'invalid') { window.location.href = domain + '/login'; return false; } 这有时有效,但有时我会收到以下浏览器警报消息: RequestAbortedError:请求已中止 我尝试
success
回调中返回{data:'invalid'}
,我使用以下内容检查此值:
if (data.status === 'invalid') {
window.location.href = domain + '/login';
return false;
}
这有时有效,但有时我会收到以下浏览器警报消息:
RequestAbortedError:请求已中止
我尝试将window.location.href
与window.location.replace()
和top.location.href
交换,但都没有解决问题。我可以看到服务器正在正确处理信息并返回{data:'invalid'}
,但只要它尝试运行行窗口。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的客户端会话npm包
服务器上的日志记录为“Passed1”和“Passed2”。它根据
get
呼叫中的状态发送客户端“Invalid”(无效)。根据对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();
}
});
如果您正在设置URL,从而离开当前页面,为什么还要麻烦返回false呢?(严肃的问题)控制台中的
window.location.href
输出是什么?@enhzflep由于我使用ajax,我认为无法从服务器重定向。我的理解是,我需要从服务器传递信息,并让客户端javascript使用ajax处理重定向。@Scaramouche不确定如何console.log
命令window.location.href
,正如我在问题中所述,重定向按预期进行。然后,尝试执行console.log(domain+'/login')
将其分配给window.location.href