在angularjs拦截器中停止请求

前端之家收集整理的这篇文章主要介绍了在angularjs拦截器中停止请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在 Angularjs拦截器中停止请求.

有什么办法吗?

我尝试使用承诺并发送拒绝而不是解决

.factory('connectionInterceptor',['$q','$timeout',function($q,$timeout) {
    var connectionInterceptor = {
        request: function(config) {
            var q = $q.defer();
            $timeout(function() {
                q.reject();
            },2000)
            return q.promise;
            // return config;
        }
    }
    return connectionInterceptor;
  }
])
.config(function($httpProvider) {
   $httpProvider.interceptors.push('connectionInterceptor');
});
$http服务有一个选项
超时做这个工作.
你可以这样做:
angular.module('myApp')
    .factory('httpInterceptor','$location',function ($q,$location) {
        var canceller = $q.defer();
        return {
            'request': function(config) {
                // promise that should abort the request when resolved.
                config.timeout = canceller.promise;
                return config;
            },'response': function(response) {
                return response;
            },'responseError': function(rejection) {
                if (rejection.status === 401) {
                    canceller.resolve('Unauthorized'); 
                    $location.url('/user/signin');
                }
                if (rejection.status === 403) {
                    canceller.resolve('Forbidden');  
                    $location.url('/');
                }
                return $q.reject(rejection);
            }

        };
    }
    ])
    //Http Intercpetor to check auth failures for xhr requests
   .config(['$httpProvider',function($httpProvider) {
        $httpProvider.interceptors.push('httpInterceptor');
    }]);
原文链接:/angularjs/142753.html

猜你在找的Angularjs相关文章