Express js重定向不呈现页面
我正在尝试重定向使用我网站的用户,在他登录到他的帐户后,他将被重定向到仪表板。Express js重定向不呈现页面,express,redirect,Express,Redirect,我正在尝试重定向使用我网站的用户,在他登录到他的帐户后,他将被重定向到仪表板。 问题是,我可以在浏览器检查工具的“网络”选项卡中看到对/dashboard路由的请求,但该页面从未加载。 这是到目前为止我的代码 router.post('/login', function(request, response){ // verify against database and send back response var userData = {}; userData.emai
问题是,我可以在浏览器检查工具的“网络”选项卡中看到对
/dashboard
路由的请求,但该页面从未加载。这是到目前为止我的代码
router.post('/login', function(request, response){
// verify against database and send back response
var userData = {};
userData.email = request.body.email;
userData.password = request.body.password;
userData.rememberPassword = request.body.rememberPassword;
// check if in the database and re-route
var query = database.query('SELECT * FROM users WHERE email = ?', [userData.email], function(error, result){
if(error){
console.log('Error ', error);
}
if(result.length){
// check if the password is correct
if(userData.password === result[0].password){
// redirect to the dashboard
// TODO this piece of code does not redirect correctly
response.redirect('/dashboard'); // < HERE
console.log('Haha');
}
}
else{
// do something when there is are no results
// invalid email status code
response.statusCode = 405;
response.send('');
}
});
});
为什么当用户完成登录过程时它不能重定向?
谢谢。问题可能在前端,而不是后端。如果您使用AJAX发送POST请求,那么它是专门为不更改您的url而设计的 AJAX请求完成后,使用
window.location.href
以所需路径更新URL。但最简单的方法是创建一个表单,操作为/login
,并使用提交触发URL更改
为了确保问题不在于后端
res.setHeader(“内容类型”,“text/html”)
进行设置router.get('/dashboard', function(request, response){
response.render(path.resolve(__dirname, 'views', 'dashboard.pug'));
});