【发布时间】:2019-07-18 00:22:02
【问题描述】:
我想从 /{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()) 所以我不知道有什么问题,如果有人知道线索,如果你告诉我,我会很高兴
【问题讨论】:
标签: node.js reactjs http axios fetch-api