gpt4 book ai didi

javascript - 从 React fetch 获取数据

转载 作者:行者123 更新时间:2023-12-03 06:04:13 25 4
gpt4 key购买 nike

我正在尝试使用此代码从 Api 帖子中获取数据

componentWillMount() {
fetch('http://url', {
method: 'POST',
body: JSON.stringify({
usuario: 'conosur2',
password: 'test',
})

})
.then((response) => {
console.log(response.json());
})

我在控制台上得到了这个

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}

enter image description here

我需要访问该数据,如何?我的意思是,类似 response.json('clientId')response.json().clientId() ,我还没有反应,所以我不知道 Promise 是如何工作的,也不知道 fetch 是如何工作的(我来自 ajax api Consumer)对不起我的英语

最佳答案

response.json() 返回一个 promise ,因此您需要解决它

componentWillMount() {
fetch('http://url', {
method: 'POST',
body: JSON.stringify({
usuario: 'conosur2',
password: 'test',
})

})
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
})

关于javascript - 从 React fetch 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39625108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com