【问题标题】:Network Error while using axios with fastapi (python) running on http://127.0.0.1:8000将 axios 与在 http://127.0.0.1:8000 上运行的 fastapi (python) 一起使用时出现网络错误
【发布时间】:2022-01-04 21:17:04
【问题描述】:

我正在尝试将 API 与我的 react 本机应用程序集成,但是当我提出请求时,我总是收到网络错误:

[Unhandled promise rejection: TypeError: Network request failed]
at node_modules\whatwg-fetch\dist\fetch.umd.js:541:17 in setTimeout$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:123:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:379:16 in callTimers
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:414:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:113:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:365:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:4 in callFunctionReturnFlushedQueue
at [native code]:null in callFunctionReturnFlushedQueue

我的 axios 配置如下:

import axios from "axios";
const baseUrl = 'http://localhost:8000/' 
export const api = axios.create({baseURL: baseUrl});

还尝试了“http://10.0.2.2:8000/”和“http://127.0.0.1:8000”,但得到了同样的错误。

有人可以帮我吗?提前谢谢你!

【问题讨论】:

    标签: react-native axios fastapi uvicorn


    【解决方案1】:

    通过查看您的 baseUrl,我猜您正在尝试访问本地托管 API。所以,为了让它在模拟器/有线设备上工作;您需要执行以下步骤:

    打开命令提示符,输入以下命令:

    ipconfig

    并记下 IPv4 地址。现在,将与基本 URL 相同的 IPv4 地址粘贴到 axios 文件中。例如

    http://YOUR-IPv4-Address-Here:YOUR-API-PORT

    我希望这能解决这个“网络请求失败”的问题。

    【讨论】:

    • 哈西布您好!感谢你的回复。由于某种原因,它不起作用。我正在使用expo构建应用程序,有什么问题吗?
    猜你喜欢
    • 2021-07-07
    • 1970-01-01
    • 2015-09-02
    • 2020-10-02
    • 2020-03-16
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多