我通过AJAX发送POST请求到服务器后一种观点:渲染在Node.js的POST请求
result.ejs
$.ajax({
type: "POST",
url: "/result",
data: { score: score, rank: rank, name: name, email: email, historyLog: historyLog }
});
我reciveing它和保存数据到数据库:
resultController.js
app.post('/result', urlencodedParser, function(req, res) {
var user = new User({
name: req.body.name,
email: req.body.email,
score: req.body.score,
rank: req.body.rank,
historyLog: req.body.historyLog
});
user.save(function(err) {
if (err) throw err;
console.log('User saved successfully!');
});
});
保存后,我想呈现一个新的视图,我无法得到它的工作。 我曾尝试保存功能后添加此:
res.redirect('page')
,res.render('page')
,设置页眉HTML /文本的上述
无工作。有什么建议么?
是的,使用表单会容易得多,但我不能在我的情况下实现它,但是将这个函数添加到ajax成功了诀窍! +1 – mirta