繁体   English   中英

无法在req.body中获取正确的数据格式

[英]Unable to get the correct data format in req.body

我正在尝试将body解析为nodeJS,但我以错误的格式将其恢复。

这是我的代码:

fetch("http://localhost:5000/email", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },

      body: JSON.stringify({ shareUrl: props.shareUrl })
    });`

这是在Node中的console.log(body.req.shareUrl)时解析它的方式

{ '{"shareUrl":"link"}': '' }

这就是我想要它的方式

{ 'shareUrl': 'link' }

编辑

当我尝试从Postman发送它时它工作正常

在此输入图像描述

我得到以下输出:

{ Name: 'jej' }

内容类型:

{ "Content-Type": "application/x-www-form-urlencoded" }

不首先发送JSON数据。

如果要发送和接收JSON,请将内容类型更改为:

{ "Content-Type": "application/json" }

喜欢:

fetch("http://localhost:5000/email", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: { data: json.stringify({shareUrl: props.shareUrl}) }
    });`

在服务器上,您可以在req.body.data下找到它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM