【问题标题】:How to show custom error message when there is not internet没有互联网时如何显示自定义错误消息
【发布时间】:2019-02-05 05:09:46
【问题描述】:

输入一些值后,当用户断开互联网并尝试执行任何操作时。我的应用程序没有显示任何错误消息,说明“没有互联网连接” 在控制台中它正在抛出

angular_1.3.13.js:11613 TypeError: 无法读取 null 的属性“错误” 在 Object.handleError (apiHelper.js:120) 在shortnoteCreateController.js:88 在 angular_1.3.13.js:9382 在 processQueue (angular_1.3.13.js:13195) 在 angular_1.3.13.js:13211 在 Scope.$eval (angular_1.3.13.js:14407) 在 Scope.$digest (angular_1.3.13.js:14223) 在 Scope.$apply (angular_1.3.13.js:14512) 完成后(angular_1.3.13.js:9665) 在 completeRequest (angular_1.3.13.js:9855)

处理以下代码中的错误。

this.handleError = function(status, data) {

        if (status == URLs.unauthorizedCode && address.location.hash != "#/login") {
            var userdet = this.getUserDetailsFromServer(URLs.HTTP_GET, URLs.requestheaders);
            userdet.success(function(data) {
                //navigate to login page when unauthorized operations performed
                //its a rare case of getting permission reverted by PA when in action
                //or if logged out in another tab or logged in as another user in another tab
                //localStorageService.set('successmsg', 'You have been prevented from an unauthorized access');
                 displayResult.showErrorResults(Constants.authorizationFailureMsg,'success');
                address.location.href = URLs.loginView;
                //address.location.reload();
                //console.info("in handle error auth fail");

            });
            userdet.error(function(data) {
                localStorageService.set('successmsg', 'You have been prevented from an unauthorized access');

                localStorageService.clearAll();
                address.location.href = URLs.loginView;
                address.location.reload();
                displayResult.showErrorResults(Constants.authorizationFailureMsg,'success');
                //console.info("in handle error auth fail");

            });
        } else if(status == URLs.notFoundCode){
            address.location.href = URLs.notFoundPage;
        }else {

            $rootScope.rs_error_status = status;
            //console.log("inside apihelper", data);
            if (data.error) {
                $rootScope.rs_error_data = data;
                //console.info("in error");
            } else if (data.errors) {
                //console.info("in errors");
                $rootScope.rs_error_data = data.errors;
            }
            var modalInstance = $modal.open({
                templateUrl : 'userinterface/pages/modal/errordialog.html',
                controller : 'modalController'
            });

            modalInstance.result.then(function() {
                //console.info("Exiting modal popup");
            }, function() {
            });

        }

【问题讨论】:

    标签: javascript html angularjs


    【解决方案1】:

    您可以使用navigator.onLine,它为在线和离线返回布尔值 true/false

    您也可以将online/offline 事件附加到窗口

    window.addEventListener('offline', (e)=> { //code here});
    
    window.addEventListener('online', (e) => { //some code here});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2018-07-21
      • 1970-01-01
      相关资源
      最近更新 更多