检查互联网连接时离子服务器连接失败
ionic server connection unsuccessful when checking internet connection
我在 ionic 上开发,但在检查是否有互联网连接时遇到问题。
这是我检查互联网连接的代码:
$scope.checkConnection = function() {
if(navigator && navigator.connection && navigator.connection.type === 'none') {alert('no conexion');}
else{alert('conectado');}
};
我在index.html中这样称呼它:
<body ng-app="myApp" ng-controller="checkNetworkController" ng-init="checkConnection()">
当我启动应用程序时,我得到:
应用程序错误 - 与服务器的连接失败。 (file:///android_asset/www/index.html)
如果我不调用 checkConnection 一切正常。
我尝试重新安装 cordova 网络插件,并添加了 config.xml:
<preference name="loadUrlTimeoutValue" value="700000" />
但没有任何效果。有什么想法吗?
使用 Cordova 插件添加 cordova-plugin-network-information 然后 window.Connection
检查可用的互联网。
if(window.Connection){
if(navigator.connection.type == Connection.NONE) {
$state.go("error");
alert("offline")
}else{
console.log(navigator.connection.type);
alert("online")
}
}
终于找到解决办法了
我使用相同的代码
if(navigator && navigator.connection && navigator.connection.type === 'none')
{alert('no conexion');}
else{alert('conectado');}
但是在函数上,我将它包含在 app.js 的 $ionicPlatform.ready(function() 中,现在它可以工作了。
我在 ionic 上开发,但在检查是否有互联网连接时遇到问题。
这是我检查互联网连接的代码:
$scope.checkConnection = function() {
if(navigator && navigator.connection && navigator.connection.type === 'none') {alert('no conexion');}
else{alert('conectado');}
};
我在index.html中这样称呼它:
<body ng-app="myApp" ng-controller="checkNetworkController" ng-init="checkConnection()">
当我启动应用程序时,我得到: 应用程序错误 - 与服务器的连接失败。 (file:///android_asset/www/index.html)
如果我不调用 checkConnection 一切正常。 我尝试重新安装 cordova 网络插件,并添加了 config.xml:
<preference name="loadUrlTimeoutValue" value="700000" />
但没有任何效果。有什么想法吗?
使用 Cordova 插件添加 cordova-plugin-network-information 然后 window.Connection
检查可用的互联网。
if(window.Connection){
if(navigator.connection.type == Connection.NONE) {
$state.go("error");
alert("offline")
}else{
console.log(navigator.connection.type);
alert("online")
}
}
终于找到解决办法了
我使用相同的代码
if(navigator && navigator.connection && navigator.connection.type === 'none')
{alert('no conexion');}
else{alert('conectado');}
但是在函数上,我将它包含在 app.js 的 $ionicPlatform.ready(function() 中,现在它可以工作了。