在React Native Expo应用程序中获取数据时,未处理的承诺拒绝网络错误

问题描述 投票:0回答:1

我正在编写一个React Native应用,用于从REST端点获取数据。我已经编写了一个Spring Boot项目,并与Postman进行了完美的测试。但是,当我使用Axios提取数据时,收到错误Unhandled promise rejection: Network Error.,这是我在Android手机(不是模拟器)上运行的项目中的一些代码: ... componentDidMount(){ console.log('hahah'); axios.get('http://localhost:8080/dish/category/MainCourse').then(res => console.log(res)); } ... 当我将localhost:8080更改为10.0.0.2:8080(这是我在网络上找到的解决方案)时,错误消失了,但控制台未记录任何JSON响应。我已经厌倦了在Spring Boot控制器中使用@CrossOrigins,但是它仍然无法正常工作。我认为问题是idk如何更改localhost:8080来更正地址。任何人都可以解决我如何解决此问题。

spring-boot react-native react-native-android
1个回答
0
投票

您应该将10.0.0.2更改为托管Spring Boot服务器的笔记本电脑/ PC /机器的IP地址。

© www.soinside.com 2019 - 2024. All rights reserved.