我正在写一个react原生应用。我能够理解如何使用axios或fetch从api数据中检索数据。但是我遇到了麻烦。我正在尝试使用我从上一个类传递的令牌,并使用它来获取新的api和数据。我可以连接到服务器,并且我在邮递员中连接正常。但是当我在Visual Studio中从终端查看我的日志时。我得到一个空数组。我得到array = []。但我知道这是用来显示数据的。
async componentDidMount(){
try {
const savedProfile = await AsyncStorage.getItem('userData');
const profile = JSON.parse(savedProfile);
const token = profile.token;
console.log(token);
const email = profile.user_email;
const name = profile.user_nicename;
fetch(url, {
method: 'GET',
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
},
Authorization: "Bearer " + token,
})
.then(response => response.json())
.then(response => console.log(response));
} catch (err) {
console.warn(err);
}
};我相信这个问题可能和我在这个网站上读到的其他帖子一样。我也看了react网站上的文档,但我可能需要更多的帮助。我首先要做的就是打印控制台中的数据。我用虚拟数据填充了我的页面。但如果可能的话,我希望用json数据的特定部分填充我的表。基本上是获取地址,这是一个例子。
"address": "4060 18th Ave NE, Naples, Florida, USA",
我渲染的其余部分看起来像这样。但我希望至少在控制台中显示数据,这样我就可以从那里开始。
render() {
return (
<View>
<Header
centerComponent={{ text: 'Schedule', style: { color: '#fff' } }}
containerStyle={{
backgroundColor: '#000000',
justifyContent: 'space-around',
}}
/>
<FlatList
data={[
{key: 'Devin'},
{key: 'Dan'},
{key: 'Dominic'},
{key: 'Jackson'},
{key: 'James'},
{key: 'Joel'},
{key: 'John'},
{key: 'Jillian'},
{key: 'Jimmy'},
{key: 'Julie'},
]}
renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
/>
</View>
);
}发布于 2019-11-15 02:34:02
以正确的格式传入头部参数,格式如下:
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
}https://stackoverflow.com/questions/58849305
复制相似问题