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

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

问题描述

我想在我的应用程序中编写一个错误处理部分我使用下面的这段代码但是当错误 500 发生时它的工作正常但是有一个小问题或可能是大问题,这就是页面加载第一次和几秒错误页面加载后, 如何删除这几秒钟并直接转到错误页面而不加载释放错误的主页?有没有办法在执行其控制器后加载 html 模板?

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);

推荐答案

我假设您使用的是 angular ui-router.

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

第一件事,您需要在 $stateProvider 配置中添加一个状态以通过 ui-router 了解错误"状态.

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

路由配置代码

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

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

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

拦截功能变化

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);

其他方式你可以试试同样的 $state.go('error');不要忘记添加 $state 依赖.

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.

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

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