我正在尝试在 react-native 中本地获取 laravel API。当我尝试使用远程 url 时,它可以工作,但不能与本地服务器一起使用。我正在使用 android studio 模拟器和react-native => 0.59.8, laravel => 5.7我的代码 async submit() { //https://facebook.github.io/react-native/movies.json => this works try { let response = await fetch('localhost:8000/api/coins'); let responseJsonData = await response.json(); console.log(responseJsonData, 'data'); } catch (e) { console.log(e, 'error') }};错误05-20 14:46:08.167 4749 8885 I ReactNativeJS: { [TypeError: Network request failed]05-20 14:46:08.167 4749 8885 I ReactNativeJS: line: 24115,05-20 14:46:08.167 4749 8885 I ReactNativeJS: column: 31,05-20 14:46:08.167 4749 8885 I ReactNativeJS: sourceURL: 'http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false' }, 'error'
2 回答

PIPIONE
TA贡献1829条经验 获得超9个赞
这可能是一个问题
iOS 在模拟器中运行,而 Android 在模拟器中运行。
在您localhost:8000/api/coins
localhost
指向代码运行的环境。
模拟器模拟真实设备,而模拟器只是模仿设备。所以localhost
在 Android 上指向模拟的 Android 设备。而不是运行服务器的机器。
解决方案 解决
方案是替换localhost
为您机器的 IP 地址。(例如:192.168.1.22)
添加回答
举报
0/150
提交
取消