html - 无法将表单数据发布到 app.js
问题描述
我知道已经问过类似的问题,但我找不到任何适合我的东西。
我正在尝试将一些 HTML 表单数据发布到我的 app.js 文件中。
这是我的 index.handlebars 文件:
<form method = 'post' action = '/refund'>
<input type = "text" name = "transId" placeholder = "transID">
<input type = "text" name = "amount" placeholder = "amount">
<input type = "submit">
这发布到以下路线:
//refund route
app.post('/refund', (req, res, next) => {
console.log(req.body.amount);
console.log(req.body.transId);
但是,我收到错误:TypeError: Cannot read property 'amount' of undefined
我已经尝试了每一种组合,只是传递req.body
,,req.params
一切都是公正的undefined
。
解决方案
将此中间件添加到您的代码中
app.use(bodyParser.urlencoded({ extended: false }));
因为您正在发送form
数据。
//myapp.herokuapp.com/refund
另外,尝试提供路由的完整 URL 路径,action
例如<form>
.
推荐阅读
- javascript - 通过浏览器下载本地存储的文件
- css - 如何垂直更改工具提示三角形的位置
- powershell - 方法调用失败,因为 system.string 不包含名为“WaitForStatus”的方法
- java - 无法使用 OkHttpClient 将自定义标头添加到请求有效负载
- javascript - 根据另一个数字的百分比从范围中选择一个数字
- python - 无法导入 FieldValue.arrayUnion
- c# - 测试完成 12.0 连接到 pid
- r - 数据帧的 dcast(reshape2 包)错误
- excel - 有什么办法可以在 VBA excel 中压缩此代码?也许使用循环或函数
- assembly - NASM 是否在 32 位 x86 生成的机器指令中包含段寄存器?