繁体   English   中英

无法从本地api React-Native获取数据

[英]Can't fetch data from local api React-Native

我有一个Web应用程序,使用Laravel中的API做出反应。 我想使用相同的API制作移动应用。 我无法获取数据,因为我收到NETWORK REQUEST FAILED错误。 这是我尝试获取数据的方式:

handleButton = () => {
    try {
        fetch(`127.0.0.1:8081/test`).then(response =>
        console.log(response)
    );
        } catch (e) {
            console.log(e);
        }
    };

我尝试将开发设置中的ip更改为我的机器ID,我尝试使用localhost而不是ip(127.0.0.1),但没有一个修复它...我似乎找不到任何其他东西。 这是错误文本:

Possible Unhandled Promise Rejection (id: 0):
TypeError: Network request failed
TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:42556:18)
    at XMLHttpRequest.dispatchEvent (blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:47998:27)
    at XMLHttpRequest.setReadyState (blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:46859:20)
    at XMLHttpRequest.__didCompleteResponse (blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:46686:16)
    at blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:46796:47
    at RCTDeviceEventEmitter.emit (blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:16875:37)
    at MessageQueue.__callFunction (blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:16488:44)
    at blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:16245:17
    at MessageQueue.__guard (blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:16442:13)
    at MessageQueue.callFunctionReturnFlushedQueue (blob:http://localhost:8081/cfc373de-8318-4df3-b46a-7b17d4926c5e:16244:14)

编辑:

我正在使用android studio的模拟器,因为我没有运行android> 5的android设备

在本地网络上使用本机的IP。

打开终端/ cmd。

如果您使用的是mac,则可以使用ifconfig ,它通常位于en0下。 你会看到像192.168.0.32或类似的东西。

如果您使用的是Windows,则可以使用ipconfig

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM