首页 > 解决方案 > 网络错误 Axios 试图从 localhost 获取数据

问题描述

我正在为 API 使用 XAMPP

我在控制台中有这个错误:

Network Error
- node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\axios\lib\adapters\xhr.js:80:22 in handleError
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:574:29 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

使用简单的代码:

const apiAsyncTest = async () => {
        try {
            const response = await axios.get('https://127.0.0.1/api/api.php');
            console.log(response);
        } catch (error) {
            console.log(error);
        }
    }
    apiAsyncTest();

已经检查了android模拟器的互联网,我将“127.0.0.1”更改为“localhost”https到http,但没有任何效果

我的页面 api 代码也包含简单的代码:

<?php 
echo 'test';

但是我在另一个网站上试过,我成功了

标签: phpreact-nativeaxios

解决方案


知道了!我将“localhost”更改为我的 IPV4 并成功运行


推荐阅读