【发布时间】:2020-04-22 15:37:59
【问题描述】:
app.js 中的代码
app.post('/compose', function(req, res){
const post = {
title: req.body.postTitle,
content: req.body.postBody
};
console.log(post);
res.redirect('/compose');
});
ejs 文件中的代码
<%- include('partials/header') -%>
<h1>Compose</h1>
<form action="/compose" method="post">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control name="postTitle">
<label>Post</label>
<textarea class="form-control name="postBody" rows="8" cols="80"></textarea>
</div>
<button class="btn btn-primary" type="submit" name="submit">Publish</button
</form>
<%- include('partials/footer') -%>
【问题讨论】:
-
console.log(req.body)可能会帮助您解决问题 -
req.body的内容是什么?你确定已经解析了正文吗(在content-type: application/x-www-form-urlencoded即app.use(bodyParser.urlencoded({extended: yourchoice}))时解析正文的形式@ -
是的,我已经这样做了......但它仍然无法解析内容
-
您是否在请求处理程序之前运行了适当的中间件来解析主体以填充
req.body?如果没有,请参阅此答案No request info in POST from html form。 -
两个输入类都有引号未闭合
标签: javascript node.js express ejs body-parser