【发布时间】:2019-02-07 16:35:09
【问题描述】:
我花了很多时间调试以下问题,但没有解决。每当我用户单击按钮并且 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')
)
}
router.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 记录正确。但重定向不起作用。我应该如何解决它?
【问题讨论】:
-
尝试使用 window.location.href 而不是 window.location.assign
-
@AyeshmanthaPerera 非常感谢您的评论。还是不行,我把方法改成了window.location.href = 'localhost:8080/scoreboard';
-
window.location.href = "/scoreboard";是方式
-
@AyeshmanthaPerera 它仍然无法正常工作。以下答案效果很好。非常感谢您的有用回答。我将您的答案标记为有用:) 谢谢
标签: javascript jquery node.js ajax ejs