【问题标题】:angularjs error handling in ajax requestajax请求中的angularjs错误处理
【发布时间】:2015-06-27 00:50:53
【问题描述】:

我想在应用程序中编写错误处理部分, 我怎样才能删除这几秒钟并直接转到错误页面而不加载释放错误的主页?有没有办法在执行其控制器后加载 html 模板?

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

【问题讨论】:

  • 是使用 $injector.get('$state').go('error');但对这个问题没有任何影响
  • 为什么您不在 $routeProvider / $stateProvider 本身中处理此类错误。
  • 我该怎么做?我是 angularjs 的新手
  • 取决于返回请求错误的时间,在返回之前您无法猜测错误,如果您在加载或加载模板时运行 ajax 请求,您将无法在此之前重定向:)跨度>
  • 因此,如果您想在模板之前重定向,您需要运行 $http 调用,例如在 routeProvider 解析函数中

标签: ajax angularjs error-handling exception-handling angularjs-routing


【解决方案1】:

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

您需要在 $stateProvider 配置中添加一个状态以了解 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

拦截功能改动

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 依赖。

希望这对您有所帮助。 如果仍有任何困惑,请告诉我。

【讨论】:

  • 我在配置中使用 hashprifix $locationProvider.hashPrefix('!'); , 和 window.location.hash 出错,我该如何解决这个问题?
  • 我使用 $state.go 和 $location 但是它们都有我之前在我的帖子中说过的问题,首先是模板加载和 ajax 请求发送但 ajax 返回 404 并且 div 保持为空,之后几秒钟页面更改为错误状态我想删除这几秒钟,当用户转到仪表板页面时(例如这里有一些错误)转到错误页面而没有任何中断
  • 使用 $state.transitionTo('error') 你试过了吗?
  • 所有这些都具有真实效果并更改状态,但我可以看到释放该错误几秒钟的页面,例如大约 1 秒,并且在该状态更改为错误状态之后,我该如何删除这个 1第二?有可能吗?
  • @mhadadi 不,我们不能这样做,只有在处理完所有请求之前显示加载图标来实现这一点
猜你喜欢
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 2012-11-19
  • 2017-07-26
  • 1970-01-01
  • 2015-08-04
  • 1970-01-01
  • 2014-01-12
相关资源
最近更新 更多