简体   繁体   English

在AngularJS中添加HTTP标头不起作用

[英]Adding HTTP header in AngularJS not working

I am new to AngularJS and I was trying to add http header to all requests but it is not working. 我是AngularJS的新手,我试图将HTTP标头添加到所有请求中,但无法正常工作。 I was actually try to add those via the .config in Angular 我实际上试图通过Angular中的.config添加那些

My code is: 我的代码是:

 angular.module('sc.app',[])
    .provider('TokenHandler',['$q',function ($q) {
        return {
            $get: function () {
                return {
                    request: function (config) {
                        var defer = $q.defer();
                        console.log('jwt');
                        config.defaults.headers.common['Authorization'] = 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==';
                        config.defaults.headers.common['Accept'] = 'application/json;odata=verbose';

                        return defer.resolve(config);
                    },
                    requestError: function (err) {
                        var defer = $q.defer();
                        return defer.reject(err);
                    },
                    response: function (success) {                            
                   var defer = $q.defer();
                    console.log(success)
                    return defer.resolve(success);
                },
                responseError: function (err) {
                    var defer = $q.defer();
                    return defer.reject(err);
                }
            };
        }
    }
}])

.config(['$httpProvider','TokenHandler',function ($httpProvider,TokenHandler) {
    $httpProvider.interceptors.push('TokenHandler');
}])

.service('TokenService',['$http','$localStorage',function ($scope,$localStorage) {
    $scope.getToken=function () {
        return $localStorage.gaid;
    }

    $scope.setToken=function (token) {
        $localStorage.gaid=token;
    }
}]);

I googled a lot but I could not solve the issue in it. 我在Google上搜索了很多,但无法解决其中的问题。 Even though I also tried to add with config.headers option but also not working. 即使我也尝试使用config.headers选项添加,但也无法正常工作。

and my request header is 我的请求标头是

GET /api/datatable.json?_=1484584475255 HTTP/1.1
Host: scrm.app
Connection: keep-alive
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36
Referer: http://scrm.app/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,ta;q=0.6
Cookie: _ga=GA1.2.550730185.1483706923; 
XSRF-TOKEN=eyJpdiI6Ik9RaDBLeWluTVBIS0ZnOWkyaWxDUXc9PSIsInZhbHVlIjoiWlMzZUxONGthTmRKbkxxQjkxMGZWTE40d1lFYnAxRkRCM254b21abUpiOVFkd3VqbXowTjhlenFYa0xSazV1WmJuK2wweUFNZjVvM05NTXU3NjAwVmc9PSIsIm1hYyI6Ijg1Yzk0OWM3MTFkNTdlYTA1NjgzYjYyMmY3NmE1MWI0OTA4Mjc1MjM1ZTdkYTUxOWUxZmZhYjFlNzRmNzc2MDkifQ%3D%3D; laravel_session=eyJpdiI6InZqb01UbDRNVFgrTG1FVitvZW1WQXc9PSIsInZhbHVlIjoic3NKUXo5ZGd5ODBpZXhNRXBsa1pmbVhFSmhUVDA3RkZyQ3FcL1VlOVRZUWt4UlVxZVlidlU4VUV5VjBCZFwvUldCbzZVNU83R25zMXpRZ25DTENTZGpsUT09IiwibWFjIjoiZjNjNWM2OWU2ZDM3NDVlNDRkYTRhMjk4MmM4NWJmNjI0ODQyMmQwMjkzZTBkZWQ3N2ZjZTg4YzdjYTljZWNjNCJ9

my response header 我的回应标题

 HTTP/1.0 404 Not Found
Host: scrm.app
Connection: close
X-Powered-By: PHP/5.6.27
Cache-Control: no-cache, private
Date: Mon, 16 Jan 2017 16:49:42 GMT
Content-type: text/html; charset=UTF-8

When adding headers via config you only need config.headers eg - 通过config添加标题时,您只需要config.headers例如-

config.headers['Authorization'] = 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==';
config.headers['Accept'] = 'application/json;odata=verbose';

Only when adding through the $http service do you need the rest eg - 只有通过$http服务添加时,才需要其余的,例如-

 $http.defaults.headers.common.Authorization = 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==';

Edit: Another way to try add the following to the .service block - 编辑:尝试将以下内容添加到.service块的另一种方法-

.service('TokenService',['$http','$localStorage',function ($scope,$localStorage) {  
     var service = this;

    service.request = function(config) {
        config.headers['Authorization'] = 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==';
        config.headers['Accept'] = 'application/json;odata=verbose';

        return config;
    };
......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM