我有一個反應前端和節點后端,我正在使用 Axios 從外部 API 獲取物件串列,然后嘗試將其傳遞給我的節點后端。問題是節點后端無法在后端接收此有效負載資料,req.body 回傳一個空物件。為了除錯,我看到了網路選項卡中發生的情況,并觀察到有效負載資料只回傳資料型別而不是實際資料,如下所示。
在此處輸入影像描述 這是我的前端代碼的樣子:
let faqlist = "";
// fetching data from one api
await axiosinstance
.get(
"https://linktotheapi/api/faq"
)
.then((res) => {
faqlist = res.data;
console.log("This is faqlist", faqlist);
console.log(typeof faqlist);
// passing the data fetched from 1st api to node/express backend.
fetch("/create-page", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: faqlist,
});
});}
uj5u.com熱心網友回復:
您可以stringify
在發送之前將物件陣列。利用JSON.stringify
const x = [{ x: 1 }, { x: 2 }];
fetch("https://httpbin.org/post", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(x)
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497311.html
標籤:javascript 节点.js 反应 获取 API 梅尔