(Error: Network Error) - Can't make a POST request from android simulator (Wifi is ON!)
(Error: Network Error) - Can't make a POST request from android simulator (Wifi is ON!)
我正在向我自己的服务器发出 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 });
});
};
什么是网络错误?我该如何解决?
非常感谢!!
你的 ROOT_URL
你想向本地主机请求什么?如果是这种情况,请尝试将 ROOT_URL
更改为 10.0.2.2:YOUR_PORT
。查看此 link 以获取有关 android 模拟器和网络的更多信息。
如果您将 API 放在本地主机中,您可以执行以下步骤。
通过输入 ifconfig
(对于 ubuntu 和 mac)或 ipconfig
(对于 windows)检查您的笔记本电脑 IP。例如你有 192.168.40.6
运行 例如你的 djangopython manage.py runserver 192.168.40.6:8000
指定你的 axios url。例如
axios.post(http://192.168.40.6:8000/rest-auth/login/
, {...})
确保您的 phone 连接到与笔记本电脑相同的 wifi,您可以在 phone 浏览器中尝试连接,方法是转至 http://192.168.40.6:8000
我正在向我自己的服务器发出 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 });
});
};
什么是网络错误?我该如何解决?
非常感谢!!
你的 ROOT_URL
你想向本地主机请求什么?如果是这种情况,请尝试将 ROOT_URL
更改为 10.0.2.2:YOUR_PORT
。查看此 link 以获取有关 android 模拟器和网络的更多信息。
如果您将 API 放在本地主机中,您可以执行以下步骤。
通过输入
ifconfig
(对于 ubuntu 和 mac)或ipconfig
(对于 windows)检查您的笔记本电脑 IP。例如你有192.168.40.6
运行 例如你的 django
python manage.py runserver 192.168.40.6:8000
指定你的 axios url。例如
axios.post(
http://192.168.40.6:8000/rest-auth/login/
, {...})确保您的 phone 连接到与笔记本电脑相同的 wifi,您可以在 phone 浏览器中尝试连接,方法是转至
http://192.168.40.6:8000