【发布时间】:2020-05-08 08:25:41
【问题描述】:
我有一个 expo react 本机应用程序,我想在另一个项目中使用我制作的 API 中的一个函数。我在 Postman 中测试了 url,并且 url 工作正常。但我收到错误:应用程序中的Network error failed。
klt_id 和 klt_name 来自 Clients 表。该 url 指向一个函数,该函数显示 Clients 表中的所有记录。
这就是我现在拥有的:
constructor(props){
super(props);
this.state ={ isLoading: true}
}
componentDidMount(){
return fetch('http://127.0.0.1:8000/api/v2/klant', )
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson)
this.setState({
isLoading: false,
dataSource: responseJson.data,
}, function(){
});
})
.catch((error) =>{
console.error(error);
});
}
render(){
if(this.state.isLoading){
return(
<View style={{flex: 1, padding: 20}}>
<ActivityIndicator/>
</View>
)
}
return(
<View style={{flex: 1, paddingTop:20}}>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <Text>{item.klt_id}, {item.klt_name}</Text>}
keyExtractor={({id}, index) => id}
/>
</View>
);
}
}
提前致谢!
【问题讨论】:
-
这能回答你的问题吗? Requesting API with fetch in javascript has CORS policy error。有无数的帖子为什么它不适合你。请阅读CORS 是什么。
-
你在用什么?模拟器还是真机?
-
我用的是真机
-
还没有找到解决办法
标签: api react-native expo