科尔多瓦检查设备上网
我有一个基于Angular(科尔多瓦)的混合移动应用程序。 我已经实现了谷歌地图API,我需要检查加载此API之前的互联网可用性。 我试过navigator.connection.type
,但这只告诉我设备是否连接到网络,而不是网络状态。我的意思是,我可以连接到一个无线网络,该网络可能没有互联网接入。 所以,搞清楚了这一点,我已经试过这样:科尔多瓦检查设备上网
$scope.checkInternetAvail = function(){
var deferred = $q.defer();
$.get("http://www.google.com.ec/blank.html").
done(function(xh){
deferred.resolve(true);
}).fail(function(er){
deferred.reject();
});
return deferred.promise;
};
但是当我火了这个功能,我得到一个CORS相关的错误:
XMLHttpRequest cannot load http://www.google.com.ec/blank.html . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:63342 ' is therefore not allowed access.
到目前为止我的想法。 任何解决方案?
谢谢!
抱歉,cordova应用程序在您的设备上工作。它不在您的网络交换机/路由器上运行。目前还没有办法可以让你的设备询问你的路由器是否有上网功能,路由器可以确认是否有。您可以在您的设备上执行什么操作来检测网络访问。
你可以做的唯一的其他事情就是发送一个http请求给类似google的东西,在设备准备好的事件或者需要的时候,如果它以准备好的状态响应,那么你的互联网是好的,否则不会。
一个函数来测试该地址,以检查设备访问互联网应该是这样的:
function testInternet(win,fail){
$.get("https://people.googleapis.com/v1/people/me/connections").done(win).fail(fail);
}
Or ,
function testInternet(win,fail){
$.ajax({
url:"https://people.googleapis.com/v1/people/me/connections",
timeout:5000, //timeout to 5s
type: "GET",
cache: false
}).done(win).fail(fail);
}
AJAX请求应答传送回401(未授权),这意味着我们有互联网接入,但我们是未经授权的那谷歌api。 这里是谷歌api
The plugin does not detect if the user has access to the Internet, but only if the user is on a network. Since your phone is on a WiFi network even though the WiFi network doesn't have access to the Internet, the plugin is performing as designed.
我已经试过了。它不会告诉你网络是否没有互联网接入。你有没有看过我的文章? – Pablo
请再试一次。 –
它并不告诉我我连接的网络是否具有互联网连接。它只是告诉我设备是否连接到网络。这是我在开头说的 – Pablo