[英]Can't make a network request in react-native
I'm using a service to access user's geolocation. 我正在使用服务来访问用户的地理位置。 The problem is that the request works ONLY when I turn on the "Network inspection" mode in the react-native debugger. 问题是,当我在react-native调试器中打开“网络检查”模式时,请求才起作用。
Otherwise it fails with an error: 否则失败并显示错误:
TypeError: Network request failed
at XMLHttpRequest.xhr.onerror (whatwg-fetch.js:504)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:580)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:394)
at XMLHttpRequest.js:507
at RCTDeviceEventEmitter.emit (EventEmitter.js:189)
at MessageQueue.__callFunction (MessageQueue.js:366)
at MessageQueue.js:106
at MessageQueue.__guard (MessageQueue.js:314)
at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:105)
For data fetching I'm using a regular fetch, but already tried XHR and axios with the same results. 对于数据提取,我使用常规提取,但已经尝试过具有相同结果的XHR和axios。
export function getUserGeoLocation() {
const url = 'https://geoip.tradesmarter.com/json';
return fetch(url)
.then(response => response.json())
.catch(error => {
console.log(error);
});
}
The same request works on a web platform that's using Angular with jsonp. 相同的请求适用于使用Angular和jsonp的Web平台。
export function getUserGeoLocation() {
const url = 'https://geoip.tradesmarter.com/json';
const opt = {
timeout: 1000,
};
const promise = new Promise((resolve, reject) =>
jsonp(url, opt, (err, data) => {
if (err) {
reject(err.message);
} else {
resolve(data);
}
})
);
return promise;
}
Has anyone ever encountered a problem like this one? 有没有人遇到像这样的问题?
你有没有为Android和ios添加网络即互联网许可
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.