【问题标题】:AxiosError: Network Error in React Native on making api request to local backendAxiosError:React Native 在向本地后端发出 api 请求时出现网络错误
【发布时间】:2022-12-04 08:53:45
【问题描述】:
Frontend

await axios.get("http://localhost:8000/admin/api/getcategories",{
            "Content-Type": "application/json",
          }).then(result=>{
            console.log(result);
        }).catch(err=>{
            console.log(err);
        })
Backend

const cors = require('cors');


app.use(cors());

尝试重新启动应用程序,并尝试使用其他 api 发送不带标题的内容,但没有任何效果,还添加了

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

对于安卓 和 NSAllowsArbitraryLoads iOS版

【问题讨论】:

  • http://localhost:8000 不适用于移动设备,它会 moslty ping 设备本身,而不是尝试找到你的开发机器的 IP 地址,打开端口 8000 并使用 http://serverIP:800 的链接

标签: node.js reactjs react-native axios


【解决方案1】:

尝试使用 ngrok (https://ngrok.com/) 公开您的本地 API。它对开发人员免费

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2021-04-03
    • 2020-10-26
    • 2019-09-04
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多