【问题标题】:ionic server connection unsuccessful when checking internet connection检查互联网连接时离子服务器连接不成功
【发布时间】:2018-07-20 20:32:16
【问题描述】:

我正在使用 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" />

但是没有任何效果。有什么想法吗?

【问题讨论】:

    标签: networking ionic-framework server connection


    【解决方案1】:

    使用 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 (window.Connection)
    • 你是否使用了这个命令 ** cordova plugin add cordova-plugin-network-information ** ?检查 console.log(navigator.connection.type) ;和 console.log(window.Connection) ;在运行中
    • 是的,我使用了那个命令。我得到 navigator.connection 未定义并且 [$rootScope:inprog] $apply 已经在进行中 errors.angularjs.org/1.5.3/$rootScope/inprog?p0=%24apply return new ErrorConstructor(message);在 ionic.bundle.js (line25000)
    • 分享你的控制台。把两者都放在 angular.run ()
    • 错误:[$rootScope:inprog] $apply 已经在进行中 errors.angularjs.org/1.5.3/$rootScope/inprog?p0=%24apply return new ErrorConstructor(message); ionic.bundle.js (linea 13438, col 12) TypeError: navigator.connection is undefined console.log(navigator.connection.type) ; app.js (line 13, col 3)
    【解决方案2】:

    我终于找到了解决办法。

    我使用相同的代码

    if(navigator && navigator.connection && navigator.connection.type === 'none')    
    {alert('no conexion');}
    else{alert('conectado');}
    

    但是在一个函数上,我将它包含在 app.js 的 $ionicPlatform.ready(function() 中,现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2017-03-16
      • 2014-02-08
      相关资源
      最近更新 更多