如何使 localhost rest api 与 react-native 一起使用? [英] How to make localhost rest api work with react-native?

查看:60
本文介绍了如何使 localhost rest api 与 react-native 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从 Spring Boot localhost restapi 中获取一些值.它正在引发网络错误.我也尝试使用 Axios.get() 获取但没有结果.我也在我的 springboot 类和方法中提供了 CORS(origins='*').

异步 componentDidMount(){const response = await fetch('http://localhost:8080/jobSeeker/allJobSeekers');const body = 等待 response.json();this.setState({ users: body, isLoading: false });控制台日志(用户);}

<块引用>

[未处理的承诺拒绝:类型错误:网络请求失败]
- 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个来自框架内部的堆栈帧

解决方案

模拟器或设备的 localhost 与计算机的 localhost 不同.您可以在两者之间映射端口,在这种情况下,adb reverse tcp:8080 tcp:8080 会将模拟器/设备上的 localhost:8080 映射到 localhost:8080 在您的计算机上.

I have been trying to fetch some values from my spring boot localhost restapi. It is throwing a network error. I have tried to fetch with Axios.get() too but no result.I have given CORS(origins='*') in my springboot class and method too.

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

解决方案

The localhost of your emulator or device is not the same as the localhost of your computer. You can map ports between the two, in this case adb reverse tcp:8080 tcp:8080 will map localhost:8080 on your emulator/device to localhost:8080 on your computer.

这篇关于如何使 localhost rest api 与 react-native 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆