angularjs错误处理Ajax请求 [英] angularjs error handling in ajax request

查看:174
本文介绍了angularjs错误处理Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个错误处理在我的应用程序的一部分我用这个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(拦截);
 

其他的方式,你可以尝试在同一$ 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.

这篇关于angularjs错误处理Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆