【发布时间】:2021-01-16 13:59:20
【问题描述】:
我正在尝试从我的 js 文件中的 html 表单接收输入。
HTML
<form action='/bmicalc' method="post">
<input type='text' name='h' placeholder='Height'>
<input type='text' name='w' placeholder='Weight'>
<button type='submit' name='sub'>Calculate</button>
</form>
JS
const express=require('express');
const app=express();
app.get('/',function(req,res){
res.send('<h1>Hello There!<h1>');
});
app.get('/bmicalc',function(req,res){
res.sendFile(__dirname+'/bmicalc.html');
})
app.post('/bmicalc',function(req,res){
console.log(req.body.h);
});
app.listen(3000,function(){
console.log("Started server on port 3000");
})
但是我总是收到以下错误:
TypeError: 无法读取未定义的属性“h”
有人可以帮忙指出错误吗?
【问题讨论】:
-
看来
req.body是undefined。所以这就是为什么你不能访问属性h。 -
但我在 html 正文标签中有表单标签。那么为什么要显示未定义呢?
标签: javascript html node.js backend