您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

angularJS 1.0.x中所有HTTP请求的拦截器

angularJS 1.0.x中所有HTTP请求的拦截器

官方文档中有一个很好的例子,适用于当前的稳定版1.2.0。

[ http://docs.angularjs.org/api/ng。$ http] [1](页面的前四分之一,搜索拦截器)

angular.module('RequestInterceptor', [])
  .config(function ($httpProvider) {
    $httpProvider.interceptors.push('requestInterceptor');
  })
  .factory('requestInterceptor', function ($q, $rootScope) {
    $rootScope.pendingRequests = 0;
    return {
           'request': function (config) {
                $rootScope.pendingRequests++;
                return config || $q.when(config);
            },

            'requestError': function(rejection) {
                $rootScope.pendingRequests--;
                return $q.reject(rejection);
            },

            'response': function(response) {
                $rootScope.pendingRequests--;
                return response || $q.when(response);
            },

            'responseError': function(rejection) {
                $rootScope.pendingRequests--;
                return $q.reject(rejection);
            }
        }
    });

您可以存储当前时间,而不用计算未决请求,可以说为lastRequestTimestamp。如果将其与全局运行的计时器结合使用,则可以检测到上一个请求是多久之前的。

其他 2022/1/1 18:15:41 有437人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶