Express req.body在表单提交中为空 [英] Express req.body is empty in form submission
本文介绍了Express req.body在表单提交中为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下表格,提交后结果为空req.body
这是我的HTML:
I have the following form and submitting it results in an empty req.body
This is my HTML:
<form action="/contact" name="contactUs" id="contactUs" method="POST">
<label for="email">Email Message</label>
<input type="text" name="email" id="email">
<input type="submit" value='Save'>
</form>
这是我的server.js
的(部分):
app.use(bodyParser.json());
app.use("/", expressStaticGzip("dist"));
app.post("/contact", (req, res) => {
console.log('anyone there?')
res.json(req.body);
});
console.log
正常工作.我得到了req.body
的空对象.
The console.log
works fine. I get an empty object for req.body
though.
推荐答案
您正在以HTML POST的形式提交,这意味着您正在使用Content-Type: application/x-www-form-urlencoded
.
You're submitting as HTML POST that means you are using Content-Type: application/x-www-form-urlencoded
.
这意味着您需要添加app.use(express.urlencoded({ extended: false }))
,这样express可以解析该请求并将其放入req.body
This means that you need to add app.use(express.urlencoded({ extended: false }))
, This way express can parse that request and put it to req.body
这篇关于Express req.body在表单提交中为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文