api - React Native 获取本地 API
问题描述
我有一个 expo react 本机应用程序,我想在另一个项目中使用我制作的 API 中的一个函数。我在 Postman 中测试了 url,并且 url 工作正常。但我得到了错误:Network error failed
在应用程序中。
klt_id
and来自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>
);
}
}
提前致谢!
解决方案
您可以ngrok
试一试:它将允许您拥有一个“正常”的 HTTPS URL,您应该可以在您的 Expo 应用程序中使用而不会出现任何问题。他们网站上的例子:
推荐阅读
- c# - PipeWriter 和长度前缀消息
- python-3.x - 低于 10 的素数之和为 2 + 3 + 5 + 7 = 17。求所有低于 200 万的素数之和。(我没有明白这段代码有什么问题)
- javascript - 仅使用代码将动态 Instagram 提要嵌入 HTML(无 API/插件)
- reactjs - 标记组件未在 React JS 中的 Google 地图上显示
- php - 以更漂亮的方式回显日期
- python - 有什么方法可以禁用或至少绕过不和谐机器人的速率限制?
- swift - 在不相关的 ViewModel 中从我的身份验证类订阅用户变量 - Swift/Combine
- python - 从派生的 kivy 类访问 Python 函数
- python - 如何在 str.format() 方法中使用变量?
- javascript - p5.js 网页编辑器。'未捕获的类型错误:无法读取未定义的属性'拆分'(:第 57 行)'