I've got error "Network request failed" when using fetch in React Native on Android emulator/real device. The code is below:
fetch('http://localhost:3000/response', {
method: 'POST',
headers: {
Accept: "aplication/json;charset=UTF-8",
"Content-Type": "application/json"
},
body:JSON.stringify({name: "abc", email: "[email protected]"}),
})
.then((response) => response.json())
.then((responseJson) => {
alert('response object:' + JSON.stringify(responseJson));
if(responseJson.valid === true){
// do something
}
})
.catch((error) => {
console.error(error);
alert(error);
});
});
I've tried to fix it by adding "app.use(cors());" on express server. This fix works when I simulate on web but still fails on emulator or real device.