由于与其他应用程序兼容的某些原因,我需要使用破折号并试图弄清楚当破折号出现时如何获取字段名称
<input type="text" name="batch-code">
我在应用程序中添加了body解析器,但似乎找不到用破折号获取该值的方法。
app.use(bodyParser.urlencoded({extended: true}));
有什么办法可以找回我吗?
TA
使用[]
符号访问。req.body['batch-code']
,请确保您的表单enctype
是application/x-www-form-urlencoded
(默认)而不是multipart/form-data
app.use(bodyParser.urlencoded({extended: true}));
app.post('/form', (req, res) => {
console.log(req.body['batch-code']);
res.json(req.body);
});
<form action="/form" method="post">
<input type="text" name="batch-code">
</form>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句