[英]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.