简体   繁体   English

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

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

I have a web application, in react using an API made in Laravel. 我有一个Web应用程序,使用Laravel中的API做出反应。 I want to make a mobile app using the same API. 我想使用相同的API制作移动应用。 I can't fetch data, because I get NETWORK REQUEST FAILED error. 我无法获取数据,因为我收到NETWORK REQUEST FAILED错误。 This is how I try to fetch data: 这是我尝试获取数据的方式:

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

I tried changing the ip in dev settings to my machine id, i tried using localhost not the ip(127.0.0.1) but none of those fixed it... I can't seem to find anything else. 我尝试将开发设置中的ip更改为我的机器ID,我尝试使用localhost而不是ip(127.0.0.1),但没有一个修复它...我似乎找不到任何其他东西。 Here is the error text: 这是错误文本:

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)

EDIT: 编辑:

I am using android studio's emulator, because i don't have an android device running android > 5 我正在使用android studio的模拟器,因为我没有运行android> 5的android设备

Use the IP of your machine on the local network. 在本地网络上使用本机的IP。

Open up terminal/cmd. 打开终端/ cmd。

If you're using a mac, you can use ifconfig and it's usually under en0 . 如果您使用的是mac,则可以使用ifconfig ,它通常位于en0下。 You will see something like 192.168.0.32 or similar. 你会看到像192.168.0.32或类似的东西。

If you're using windows, you can use ipconfig . 如果您使用的是Windows,则可以使用ipconfig

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

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