【问题标题】:Can't fetch data from local api React-Native无法从本地 api React-Native 获取数据
【发布时间】:2019-11-03 01:51:53
【问题描述】:

我有一个 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设备

【问题讨论】:

    标签: api react-native fetch


    【解决方案1】:

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

    打开终端/cmd。

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

    如果你使用的是windows,你可以使用ipconfig

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 2018-11-07
      • 1970-01-01
      • 2022-08-23
      • 2021-09-26
      • 2021-12-23
      • 1970-01-01
      • 2021-07-26
      相关资源
      最近更新 更多