[英]Redirect doesn't work using node.js and ejs
我花了很多时间调试以下问题,但没有解决。 每当我用户单击按钮并且 window.locatoin.redirect 和 window.location.assign 方法不起作用时,就会出现问题。
这是我的代码 index.ejs:
function registration() {
$.post('/user/register', {
teamName: $('#teamName').val(),
faculty: $('#faculty').val(),
email: $('#emailSignUp').val(),
password: $('#passwordSignUp').val()
}).done(
window.location.assign('/scoreboard')
)
}
路由器.js:
router.post('/user/register', (req, res) => {
var newUser = {
teamName: req.body.teamName,
faculty: req.body.faculty,
email: req.body.email,
password: req.body.password
}
userModel.create(newUser, (err, user) => {
if (err) {
console.log('[Registratoin]: ' + err);
} else {
console.log('[Registration]: Done');
req.session.userID = user._id;
res.redirect('/scoreboard')
}
});
})
router.get('/scoreboard', (req, res) => {
userModel.find({}).sort('-score').exec((err, teams) => {
if (err) {
console.log('[scoreboard]: ' + err)
}
else{
res.render('main/scoreboard', {
teamInformation: teams
}
)
}
})
})
当我测试代码时,我看到[Registration]: Done
正确记录。 但重定向不起作用。 我该如何解决?
请使用 location.href 而不是 location.assign 如下:
window.location.href = "/scoreboard";
HTML5 引入了history.pushState()
和history.replaceState()
方法,它们分别允许您添加和修改历史条目。
history.pushState(null, null, "/scoreboard")
有关处理此类问题的 HTML5 新功能的更多信息,请访问此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.