【问题标题】:React Native Android HTTP Fetch FailsReact Native Android HTTP Fetch 失败
【发布时间】:2021-12-29 21:59:44
【问题描述】:

在带有本地服务器的 android 模拟器中使用 fetch 时遇到问题。我可以在节点和其他环境中毫无问题地运行以下代码,但在 Android 模拟器中,我收到错误消息。这是我正在使用的代码:

fetch("http://127.0.0.1:5050",{
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: data}
).then(()=>{
    console.log("Got Response")
}).catch((error)=>{
    console.log(error)
});

我得到的错误如下:

Possible Unhandled Promise Rejection (id: 6):
TypeError: Network request failed
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.reactapp&modulesOnly=false&runModule=true:25395:33
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.reactapp&modulesOnly=false&runModule=true:29610:26
_callTimer@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.reactapp&modulesOnly=false&runModule=true:29530:17
callTimers@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.reactapp&modulesOnly=false&runModule=true:29731:19
__callFunction@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.reactapp&modulesOnly=false&runModule=true:3081:36
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.reactapp&modulesOnly=false&runModule=true:2805:31
__guard@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.reactapp&modulesOnly=false&runModule=true:3032:15
callFunctionReturnFlushedQueue@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.reactapp&modulesOnly=false&runModule=true:2804:21
callFunctionReturnFlushedQueue@[native code]

其他 fetch 命令可以工作,例如 fetch('https://jsonplaceholder.typicode.com/todos/1')...,我认为这与使用 https 有关,但我不知道如何为同一台计算机上的服务器执行此操作。后端使用的是烧瓶,我想避免改变它。任何解决此问题的帮助将不胜感激。

【问题讨论】:

    标签: javascript android json react-native fetch


    【解决方案1】:

    您需要将127.0.0.1 替换为您的网络IP 地址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-06
      • 2018-06-16
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多