【问题标题】:node js req.body is undefined节点 js req.body 未定义
【发布时间】:2019-02-20 20:35:52
【问题描述】:

首先我试过thisthisthis

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'})) 发送请求时

标签: node.js post


【解决方案1】:
let options = {
    headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2016-10-18
    相关资源
    最近更新 更多