【发布时间】:2019-02-20 20:35:52
【问题描述】:
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.post('/user_create', (req,res) => {
console.log("trying to create a user "+req.body);
const firstName = req.body.first_name
const lastName = req.body.last_name
console.log("first name is "+firstName+" "+lastName);
res.end()
})
现在在尝试了上面给出的所有帖子之后,我仍然无法定义 req.body,我是 Nodejs 的新手。我正在使用 express 4.16.4 和 body-parser 1.18.3。
【问题讨论】:
-
请尝试使用“邮递员”手动测试您的请求。您实际上是如何将数据发送到服务的。根据您发送它们的方式,您需要另一种方式... req.params / etc...
-
@jcuypers 我正在通过 html 表单发送数据
-
@jcuypers 我使用了“邮递员”,这可以工作,但为什么使用表单无法正常工作
-
试试这样的东西。 {headers: new Headers({'Content-Type': 'application/x-www-form-urlencoded'})) 发送请求时