我正在尝试调用一个从React Native App获取的应用程序接口,但由于某种原因,它没有记录响应数据(console.warn(' data ',data))。它会打印“call to getArtists”日志,但随后什么也不会发生。
const URL = 'https://jsonplaceholder.typicode.com/posts'
function getArtists(){
console.log('call to getArtists')
return fetch(URL)
.then(response => response.json())
.then(data => {
console.warn('data', data)
})
}
代码可在此处获得:https://snack.expo.io/rkzea2Zlm at components/api-client.js
我做错了什么?
发布于 2018-06-05 04:11:59
首先在你的"api_client.js“中放一个回车,就像下面这样的代码。
function getArtists(){
console.log('call to getArtists')
return fetch(URL)
.then(response => response.json())
.then(data => {
return data
})
}
在App.js中,只需在componentWillMount中执行此操作即可。
componentDidMount(){
getArtists()
.then(data => {
alert(JSON.stringify(data))
});
}
https://stackoverflow.com/questions/50670063
复制相似问题