我一直在尝试从我的 Spring Boot localhost RestAPI 获取一些值。它引发网络错误。我也尝试使用 Axios.get() 获取,但没有结果。我也在我的 springboot 类和方法中给出了 CORS(origins='*') 。
async componentDidMount()
{
const response = await fetch('http://localhost:8080/jobSeeker/allJobSeekers');
const body = await response.json();
this.setState({ users: body, isLoading: false });
console.log(users);
}
[Unhandled promise rejection: TypeError: Network request failed]
- node_modules\react-native\Libraries\vendor\core\whatwg-fetch.js:504:29 in onerror
- node_modules\event-target-shim\lib\event-target.js:172:43 in dispatchEvent
- ... 8 more stack frames from framework internals
最佳答案
模拟器或设备的 localhost
与计算机的 localhost
不同。您可以在两者之间映射端口,在这种情况下,adb reverse tcp:8080 tcp:8080
会将模拟器/设备上的 localhost:8080
映射到 localhost:8080
在您的计算机上。
关于java - 如何使 localhost Rest api 与 React-Native 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57560548/