Having issues using fetch in the android emulator with a local server. I can run the below code in node and in other environments with no issues, but in the Android emulator, I get an error. This is the code I am using:
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)
});
The error I get is the following:
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]
Other fetch commands work such as fetch('https://jsonplaceholder.typicode.com/todos/1')...
and I assume this has to do with it using https
but I don't see how I could do that for a server on the same computer. The backend is using flask and I would like to avoid changing that. Any help fixing this would be appreciated.
CodePudding user response:
You need to replace 127.0.0.1
with your network IP Address.