John Baek John Baek - 5 months ago 54
React JSX Question

(Error: Network Error) - Can't make a POST request from android simulator (Wifi is ON!)

I'm making a POST request to my own server and my server doesn't even receive any request (no request was shown in server terminal).

I can use an internet on browser inside of the simulator. But it's giving me the error below.

Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:87)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:546)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
at XMLHttpRequest.js:485
at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
at MessageQueue.__callFunction (MessageQueue.js:260)
at MessageQueue.js:101
at MessageQueue.__guard (MessageQueue.js:228)


And below is my POST request using axois

export const doAuthLogin = ({ username, password }) => async dispatch => {
axios.post(`${ROOT_URL}/rest-auth/login/`, {
username,
password
}).then(response => {
console.log(response);
// Save Token post is Already await.
AsyncStorage.setItem('auth_token', response.token);
dispatch({ type: AUTH_LOGIN_SUCCESS, payload: response.token });
})
.catch(response => {
console.log(response);
dispatch({ type: AUTH_LOGIN_FAIL, payload: response.non_field_errors });
});
};


What is Network Error? And how can I fix it?

Thank you so much!!

Answer Source

What is your ROOT_URL are you trying to do request to localhost? If it's the case try changing ROOT_URL to 10.0.2.2:YOUR_PORT. Check this link for more information about android emulator and networking.