React Native Android HTTP 获取失败

React Native Android HTTP Fetch Fails

在本地服务器的 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('https://jsonplaceholder.typicode.com/todos/1')...,我认为这与使用 https 有关,但我不知道如何为同一台计算机上的服务器执行此操作。后端正在使用烧瓶,我想避免改变它。任何解决此问题的帮助将不胜感激。

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