[英]Undefined while console.log()
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') -%>
输出:输出未定义
<button class="btn btn-primary" type="submit" name="submit">Publish</button>
而不是这个,请尝试使用下面的代码
<input type="submit" class="btn btn-primary" name="submit"/>
req 中的空体总是因为缺少bodyparser
。
请添加app.use(express.urlencoded());
在 app 声明之后启用解析你的身体。
PS:您不能提供 HTML 按钮类型的提交。 您可以使用输入类型提交或表单末尾的简单按钮来触发提交。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.