在帶有本地服務器的 android 模擬器中使用 fetch 時遇到問題。我可以在 node 和其他環境中運行以下代碼而沒有問題,但在 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但我不知道如何為同一臺計算機上的服務器執行此操作。后端正在使用燒瓶,我想避免改變它。任何幫助解決這個問題將不勝感激。
uj5u.com熱心網友回復:
您需要替換127.0.0.1為您的網路 IP 地址。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/360655.html
標籤:javascript 安卓 json 反应原生 拿来
上一篇:如何將CSV資料轉換為字典
