简体   繁体   English

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

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

I'm trying to parse the body to nodeJS but I get it back in the wrong format. 我正在尝试将body解析为nodeJS,但我以错误的格式将其恢复。

This is my code: 这是我的代码:

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

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

This is how it is parsed when console.log(body.req.shareUrl) in Node 这是在Node中的console.log(body.req.shareUrl)时解析它的方式

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

this is how I want it back 这就是我想要它的方式

{ 'shareUrl': 'link' }

EDIT 编辑

When I try to send it from Postman it works fine 当我尝试从Postman发送它时它工作正常

在此输入图像描述

I get following output: 我得到以下输出:

{ Name: 'jej' }

The Content type: 内容类型:

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

is not ment to send the JSON data in the first place. 不首先发送JSON数据。

If you want to send and receive JSON, change the content type to: 如果要发送和接收JSON,请将内容类型更改为:

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

like: 喜欢:

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

On server you will find it under req.body.data 在服务器上,您可以在req.body.data下找到它

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

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