我正在嘗試從 express 中獲取資料以使用 fetch api 做出反應。兩者都在本地主機上運行。這是我的后端代碼:
app.get("/profile",(req,res)=>{
const data = [{
name:"shekhar",
class:"5"
}]
res.send(JSON.stringify(data));
});
React code:
function handleSubmit(){
fetch("http://localhost:4000/profile")
.then((response)=>{
console.log((response));
})
.catch(err=>{console.log(err)})
}
handleSubmit();
function handleSubmit(){
fetch("http://localhost:4000/profile")
.then((response)=>{
console.log((response));
})
.catch(err=>{console.log(err)})
}
handleSubmit();
The result it is showing in react console:
Response {type: 'cors', url: 'http://localhost:4000/profile', redirected: false, status: 200, ok: true, …}
但它沒有顯示快遞物件。但它顯示了一個回應物件。我在這里嘗試了幾個答案,但我無法解決問題。請幫我做什么我是新手。泰
uj5u.com熱心網友回復:
它將回傳一個承諾而不是實際資料,以獲取您需要像這樣解決承諾的實際資料
fetch("http://localhost:4000/profile")
.then(response => response.json())
.then(data => {
console.log(data);
});
uj5u.com熱心網友回復:
嘗試這個
fetch("http://localhost:4000/profile")
.then((response)=>{
response.json());
})
.catch(err=>{console.log(err)}).then((res)=> {console.log(res)})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/507658.html
上一篇:Nodejs上的回應正文鍵不完整
