[英]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 命令可以工作,例如fetch('https://jsonplaceholder.typicode.com/todos/1')...
我認為這與使用https
,但我不知道我該怎么做同一台計算機上的服務器。 后端使用的是 flask,我想避免改變它。 任何解決此問題的幫助將不勝感激。
您需要將127.0.0.1
替換為您的網絡 IP 地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.