如何使用新的$ http请求重写$ httpProvider config.headers中的标头

时间:2014-10-10 09:27:17

标签: javascript angularjs xmlhttprequest

在AngularJS中,我有默认标头的配置(用于身份验证) 它工作正常,每个$ http请求都带有正确的标题 但是对于某些请求,我需要将此标头更改为其他值。但我不能!我只能添加新的,但不能重写以前的值 谢谢!

这里有数千行代码,因此这个问题最重要的部分是

    //THIS PART OF CONFIG
.config(['$locationProvider',
        '$stateProvider',
        '$urlRouterProvider',
        '$httpProvider',
        function ($locationProvider, $stateProvider, $urlRouterProvider, $httpProvider){
            //....
            $httpProvider.interceptors.push('AuthInterceptor');
        }

// THIS FACTORY
.factory('AuthInterceptor', ['$q',
    function($q) {
        return {
            request: function(config) {
                config.headers = config.headers || {};
                config.headers.Authorization = 'OldValue';
                return config || $q.when(config);
            },
            response: function(response){
                if (response.status === 401) {
                    console.log('Response 401');
                }
                return response || $q.when(response);
            },
            responseError: function(rejection) {
                if (rejection.status === 401) {
                    console.log('Response Error 401',rejection);
                }
                return $q.reject(rejection);
            }
        };
    }
])

//THIS PART OF CONTROLLER
$scope.makeRequest = function(){
    var request = $http({
        method: 'GET',
        headers: {
            'Authorization' : 'NewValue', // do not work
            'NewAuthorisatnion' : 'AnotherValue' // works fine
        url: '/'

    });
    request.
        success(function (data) {
            //...
        });
}

0 个答案:

没有答案