我正在编写一个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
来更正地址。任何人都可以解决我如何解决此问题。
您应该将10.0.0.2
更改为托管Spring Boot服务器的笔记本电脑/ PC /机器的IP地址。