如何跳过HTTP请求的angularjs拦截器? [英] How to skip angularjs interceptor for an HTTP request?

查看:417
本文介绍了如何跳过HTTP请求的angularjs拦截器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个angularjs应用程序,其中有一个拦截器,该拦截器将授权令牌添加到每个请求的标头中.

I have an angularjs app, in which I have an interceptor that adds the authorization token to the header of each request.

但是,在应用程序中的某个地方,我需要使用拦截器破坏它的外部API,因为它添加了此授权标头,而该授权标头是该外部API提供程序不可接受的.仅在这种特定情况下,如何才能使angularjs HTTP跳过拦截器?

However, somewhere in the application I need to use and external API where the interceptor ruins it, because it adds this authorization header which is not acceptable by this external API provider. How can I make angularjs HTTP skip the interceptor, only on this one specific case?

拦截器代码如下:

app.factory('authInterceptorService', ['$q', '$injector', '$location', 'localStorageService', function ($q, $injector, $location, localStorageService) {
    var authInterceptorServiceFactory = {};
    var $http;

    var _request = function (config) {

        config.headers = config.headers || {};

        var authData = localStorageService.get('authorizationData');
        if (authData) {
            //console.log("token: " + authData.token.substring(0, 10));
            //console.log("user: " + authData.userName);
            config.headers.Authorization = 'Bearer ' + authData.token;
        }
        return config;
    }

    var _responseError = function (rejection) {
        var deferred = $q.defer();
        if (rejection.status === 401) {
            var authService = $injector.get('authService');
            authService.refreshToken().then(function (response) {
                _retryHttpRequest(rejection.config, deferred);
            }, function () {
                authService.logOut();
                $location.path('/login');
                deferred.reject(rejection);
            });
        } else {
            deferred.reject(rejection);
        }
        return deferred.promise;
    }

    var _retryHttpRequest = function (config, deferred) {
        console.log('retrying');
        $http = $http || $injector.get('$http');
        $http(config).then(function (response) {
            deferred.resolve(response);
            //console.log("success:" +response);
        }, function (response) {
            deferred.reject(response);
            //console.log("error:" + response);
        });
    }

    authInterceptorServiceFactory.request = _request;
    authInterceptorServiceFactory.responseError = _responseError;

    return authInterceptorServiceFactory;
}]);

推荐答案

简单

$http.get("url" , {noAuth : true}).then(success(),error());

在拦截器中

  var _request = function (config) {

    config.headers = config.headers || {};

    var authData = localStorageService.get('authorizationData');
    if (authData && !config.noAuth) {
        //console.log("token: " + authData.token.substring(0, 10));
        //console.log("user: " + authData.userName);
        config.headers.Authorization = 'Bearer ' + authData.token;
    }
    return config;
}

这篇关于如何跳过HTTP请求的angularjs拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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