(错误:网络错误) - 无法从Android模拟器发出POST请求(Wifi已开启!)

时间:2017-08-17 07:45:37

标签: reactjs react-native axios

我向我自己的服务器发出POST请求,而我的服务器甚至没有收到任何请求(服务器终端中没有显示请求)。

我可以在模拟器内部的浏览器上使用互联网。但是它给了我下面的错误。

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)

以下是我使用axois

的POST请求
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 });
    });
};

什么是网络错误?我该如何解决?

非常感谢!!

2 个答案:

答案 0 :(得分:2)

您尝试向localhost请求的ROOT_URL是什么?如果是这种情况,请尝试将ROOT_URL更改为10.0.2.2:YOUR_PORT。有关Android模拟器和网络的更多信息,请查看此link

答案 1 :(得分:0)

如果您将API放在localhost中,则可以执行以下步骤。

  1. 键入ifconfig(对于ubuntu和mac)或ipconfig(对于Windows),检查笔记本电脑的IP。例如,您获得了192.168.40.6

  2. 运行您的django,例如python manage.py runserver 192.168.40.6:8000

  3. 指定您的axios网址。例如

    axios.post(http://192.168.40.6:8000/rest-auth/login/,{...})

  4. 确保您的手机与笔记本电脑连接到同一个wifi,然后您可以在手机浏览器中尝试连接,转到http://192.168.40.6:8000

相关问题