我有一个应用程序,用户可以在其中登录并查看一些信息。
问题:
用户能够在更改网络之前登录并查看信息并注销任意次数。但是一旦用户更改网络(从wifi到移动设备或反之亦然)并尝试登录和获取信息。它的投掷错误: XMLHttpRequest.xhr.onerror网络请求失败(fetch.js:441)
注意:我正在使用fetch api进行网络通话。
获取Api电话:
export const request = async function request(path, body = null, method = 'GET') {
try {
const headers = {
'Content-Type': body instanceof FormData ? 'multipart/form-data' : 'application/json',
};
const token = await AsyncStorage.getItem('authToken');
if (token) {
headers.Authorization = token;
}
const config = {
method,
url: Config.API_URL + path,
headers,
};
if (!['HEAD', 'GET'].includes(method.toUpperCase())) {
config.body = body instanceof FormData ? body : JSON.stringify(body);
}
const response = await fetch(Config.API_URL + path, config);
const data = await response.json();
if (response.status >= 400) {
throw data.error;
}
return data;
} catch (e) {
console.log('Error', path, e);
return Promise.reject(e);
}
错误
Network request failed
at XMLHttpRequest.xhr.onerror (fetch.js:441)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:567)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:397)
at XMLHttpRequest.js:503
at RCTDeviceEventEmitter.emit (EventEmitter.js:179)
at MessageQueue.__callFunction (MessageQueue.js:351)
at MessageQueue.js:116
at MessageQueue.__guardSafe (MessageQueue.js:314)
SDK版本:
compileSdkVersion:25
buildToolsVersion:" 25.0.2"
minSdkVersion:16
targetSdkVersion:25
我是初学者,反应原生。无法识别问题。任何帮助将不胜感激。 感谢