gpt4 book ai didi

node.js - axios 不能使用,而 fetch 可以

转载 作者:可可西里 更新时间:2023-11-01 16:48:23 26 4
gpt4 key购买 nike

我想从/{url} 获取数据(数组),我试过这段代码

// Fetch the list on first mount
componentDidMount() {
this.getList();
}

// Retrieves the list of items from the Express app
getList = () => {

fetch('/main')
.then(res => res.json())
.then(list => this.setState({ list }))
}

这工作正常,但后来我决定切换到 axios 并尝试使用完全相同的代码

  // Fetch the list on first mount
componentDidMount() {
this.getList();
}

// Retrieves the list of items from the Express app
getList = () => {
axios.get('/main')
.then(res=> res.json())
.then(list => this.setState({list}))

}

但它没有用,并在这一行中给我错误:.then(res=> res.json())所以我不知道有什么问题如果有人知道线索如果你告诉我我会很高兴

最佳答案

// Fetch the list on first mount
componentDidMount() {
this.getList();
}

// Retrieves the list of items from the Express app
getList = () => {
axios.get('/main')
.then(res=> res.data)
.then(list => this.setState({list}))
.catch(error => this.setState({error: error.message}))
}

关于node.js - axios 不能使用,而 fetch 可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54851944/

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