Node中的req.body为空 [英] req.body is empty in Node

查看:89
本文介绍了Node中的req.body为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反应(客户端)通过axios发送帖子数据.但是req.body在节点服务器端为空.尝试使用身体解析器.但失败了.在此处附加了客户端

React (Client) sent post data through axios. but req.body is empty in Node server side. Tried to use body-parser. but failed. attached client side here

在此处附加了服务器代码

这是客户端Axios的一部分

推荐答案

它应该是请求中的Content-Type.

It should be the Content-Type on the request.

默认情况下,主体解析器"urlencoded"仅处理以下内容:

Per default the body-parser "urlencoded" handles only the following:

Content-Type: application/x-www-form-urlencoded;

您可以将类型设置为:

app.use(bodyParser.urlencoded({
  extended: true,
  type: 'multipart/form-data'
}))

但是随后,您必须自己解析原始主体",因为主体解析器不支持多部分.

But then you have to parse the "raw body" by yourself, because the body-parser doesn't support multipart.

这篇关于Node中的req.body为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆