【问题标题】:req body is always empty on post express js在 post express js 上,req 正文始终为空
【发布时间】:2020-10-29 01:16:16
【问题描述】:

我正在尝试使用 ExpressJS 在 POST 方法上获取 req.body

但我总是可以得到空的 json 对象 ({})。

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.post("/", (req, res) => {
  console.log(req.body) // {}
  res.send(200, req.body);  // {}
});

谁能帮帮我?

我正在使用: “正文解析器”:“^1.19.0”, "快递": "^4.17.1",

【问题讨论】:

  • 仅供参考,app.use(bodyParse.json()); 拼写错误,应为 app.use(bodyParser.json());

标签: express post body-parser


【解决方案1】:

您的提交表单是什么样的?您需要确保 POST 请求命中“/”路由。

你可以用 Postman 手动试试看能否通过。

【讨论】:

  • 我尝试使用 Postman 进行测试。但我无法得到结果。
  • 确保您以 json 格式发送请求,这是它所期望的。
【解决方案2】:

我在 Postman 上将“内容类型”更改为“X-www-form-url-encoded”。 然后我可以展示正确的身体。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 2015-09-08
    • 2017-04-20
    • 2014-05-09
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多