angularjs错误处理Ajax请求错误、angularjs、Ajax

由网友(梦里梦见他梦她°)分享简介:我想写一个错误处理在我的应用程序的一部分我用这个code以下,但是当错误500发生的工作权利,但有一个小的或者大的问题,这就是网页加载在第一,几秒钟后,错误页面加载,如何删除这个几秒钟,并直接转到错误页面,而无需加载炫魅的释放错误?有没有什么办法来执行其控制后加载HTML模板? VAR拦截= ['$ rootScope...

我想写一个错误处理在我的应用程序的一部分我用这个code以下,但是当错误500发生的工作权利,但有一个小的或者大的问题,这就是网页加载在第一,几秒钟后,错误页面加载,如何删除这个几秒钟,并直接转到错误页面,而无需加载炫魅的释放错误?有没有什么办法来执行其控制后加载HTML模板?

  VAR拦截= ['$ rootScope,$ Q',功能(范围,$ Q){

        功能成功(响应){
            返回响应;
        }

        功能错误(响应){
            VAR状态= response.status;
            如果(状态== 500){
              了window.location =HTTP://www.domain.lan/# /错误;


                返回;
            }
             如果(状态== 403){

                //了window.location =仪表板;
                返回;
            }
            // 除此以外
            返回$ q.reject(responseInterceptors);

        }

        复位功能(承诺){
            返回promise.then(成功,错误);
        }

    }];
    $ httpProvider.responseInterceptors.push(拦截);
 

解决方案

我假设你正在使用的角度UI路由器。

1日的事情,你需要在你的$ stateProvider配置添加一个状态,了解通过UI路由器的错误状态。

路由配置code

  //添加状态,了解错误
$ stateProvider.state(错误:{
   网址:/错误,
   templateUrl:/views/error.html,
   控制器:errorCtrl'//如果你想要的任何特定功能可选
});
 

和你做了window.location并设置URL,window.location的导致页面刷新。而不是使用window.location的使用window.location.hash的

拦截功能的变化

  VAR拦截= ['$ rootScope,$ Q,$位置,功能(范围,$ Q $位置){
功能成功(响应){
    返回响应;
}

功能错误(响应){
    VAR状态= response.status;
    如果(状态== 500){
      //window.location=HTTP://www.domain.lan/# /错误!;
      //window.location.hash=/错误; //这将改写无需刷新页面的网址
      $ location.path('错误');
      返回;
    }
     如果(状态== 403){

        //了window.location =仪表板;
        返回;
    }
    // 除此以外
    返回$ q.reject(responseInterceptors);

}

复位功能(承诺){
    返回promise.then(成功,错误);
}

}];
$ httpProvider.responseInterceptors.push(拦截);
 
angularjs和ajax的组合使用

其他的方式,你可以尝试在同一$ state.go('错误');不要忘了加$状态扶养。

希望这对您有所帮助。 让我知道是否还有任何混淆。

I want to write an error handling part in my application I use this code below but when error 500 occur its work right but there is a small or maybe big problem and thats the page load at first and after few second error page load , How can i remove this few second and go to error page directly without loading mainpage that release error? is there any way to load html template after execution of its controller?

var interceptor = ['$rootScope', '$q', function (scope, $q) {

        function success(response) {
            return response;
        }

        function error(response) {
            var status = response.status;
            if (status == 500) {
              window.location= "http://www.domain.lan/#!/error";


                return;
            }
             if (status == 403) {

                // window.location = "dashboard";
                return;
            }
            // otherwise
            return $q.reject(responseInterceptors);

        }

        return function (promise) {
            return promise.then(success, error);
        }

    }];
    $httpProvider.responseInterceptors.push(interceptor);

解决方案

I'm assuming that you are using angular ui-router.

1st thing you need to add one state in your $stateProvider config to understand 'error' state by ui-router.

Route config Code

//added state to understand error
$stateProvider.state("error": {
   url: "/error",
   templateUrl: '/views/error.html',
   controller: 'errorCtrl' //optional if you want any specific functionality
});         

And You did window.location and set the url, window.location causing page to refresh. Instead of using window.location use window.location.hash

Interception function change

var interceptor = ['$rootScope', '$q', '$location', function (scope, $q, $location) {
function success(response) {
    return response;
}

function error(response) {
    var status = response.status;
    if (status == 500) {
      //window.location= "http://www.domain.lan/#!/error";
      //window.location.hash= "/error"; //it will rewrite the url without refreshing page
      $location.path('error');
      return;
    }
     if (status == 403) {

        // window.location = "dashboard";
        return;
    }
    // otherwise
    return $q.reject(responseInterceptors);

}

return function (promise) {
    return promise.then(success, error);
}

}];
$httpProvider.responseInterceptors.push(interceptor);

Other way you can try the same $state.go('error'); don't forget to add $state dependancy.

Hope this will be helpful to you. Let me know if there is still any confusion.

阅读全文

相关推荐

最新文章