我想在 post 請求中將一個物件從后端發送到前端。我在前端得到物件,但那里沒有資料。
這是我在前端的功能(Vue 3):
backend2(e){
e.preventDefault();
fetch('http://localhost:5000/enemystrongest', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({enemyCards: this.enemysCards})})
.then((res) => {return res})
.then((res) => {console.log(res)})
},
這是我的后端 root.js (NodeJS):
router.post('/enemystrongest', (req, res, next) => {
let cards = req.body;
res.setHeader("Content-Type", "application/json")
res.send(findEnemyStrongest(cards));
});
然后我在開發工具/控制臺中得到這個:
Response {type: 'cors', url: 'http://localhost:5000/enemystrongest', redirected: false, status: 200, ok: true, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "http://localhost:5000/enemystrongest"
[[Prototype]]: Object
誰能幫我?
uj5u.com熱心網友回復:
您必須決議正文,這可以通過使用來完成:(res.json()這會回傳一個承諾)
backend2(e){
e.preventDefault();
fetch('http://localhost:5000/enemystrongest', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({enemyCards: this.enemysCards})})
.then((res) => {return res.json()})
.then((json) => {console.log(json)}) // as @jub0bs said this can be shortend as .then(console.log);
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406249.html
標籤:
