这是我在 react 端所做的:
componentDidMount(){
console.log('zzzzzzzzzzz')
fetch('http://localhost:5000/z', {mode: 'no-cors'}).then(res=>{console.log(res)
this.setState(res)})
}
这是我用 NodeJS + Express 所做的:
app.get('/z', (req, res)=>{
console.log('requested')
res.send({msg: "YEEEEHAW"})
})
请求确实通过了,但这不是我想要的(在浏览器控制台中找不到“YEEEEHAW”):
Response {type: "opaque", url: "", redirected: false, status: 0, ok: false, …}
type: "opaque"
url: ""
redirected: false
status: 0
ok: false
statusText: ""
headers: Headers
__proto__: Headers
body: (...)
bodyUsed: false
__proto__: Response
type: (...)
url: (...)
redirected: (...)
status: (...)
ok: (...)
statusText: (...)
.......
我这里做错了什么?我该如何解决? (明确地说,我确实查看并展开了记录对象中的每个可扩展字段,所以我很确定我没有错过它)
最佳答案
要读取响应内容,可以使用res.json()
:
componentDidMount(){
console.log('zzzzzzzzzzz')
fetch('http://localhost:5000/z', {mode: 'no-cors'})
.then(res=>res.json())
.then(res=>{
console.log(res)
this.setState(res)
})
}
关于javascript - 非常基本的 React + NodeJS 获取 : Why is my response not what I want?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524586/