如何在angularjs资源中传递头值

如何在angularjs资源中传递头值,angularjs,Angularjs,我在我的资源中定义了自定义post方法。其中一个帖子需要标题。我只想从控制器中动态设置标题值。编写的代码不起作用。它没有设置标题 服务 return $resource(Config.getURL() + '/api/:request/', { request: '@request' }, { getmylocation:{ method:'GET', params:{ request:'getmyl

我在我的资源中定义了自定义post方法。其中一个帖子需要标题。我只想从控制器中动态设置标题值。编写的代码不起作用。它没有设置标题

服务

return $resource(Config.getURL() + '/api/:request/', {
        request: '@request'
    },
    {
      getmylocation:{
        method:'GET',
        params:{
          request:'getmylocation'
        },
        headers:{
          'token':token
      }

    });
控制器

var token ="ad234saf3dsf32442werw"
apiService.getmylocation(token)

我将创建一个服务来拦截请求,并添加一个带有令牌的头,如下所示:

angular.module('common').factory('xsrfTokenInterceptor', function(principal, $location) {
    return {
        'request': function(config) {
            //principal is the service that stores my user information and his token
            var identity = principal.identity();
            if (identity !== "undefined" && identity !== undefined && identity !== null) {
                var sessionToken = identity.session;

                if (sessionToken !== "undefined" && sessionToken !== undefined) {
                    config.headers['X-Csrf-Token'] = sessionToken;
                }
                return;
            } else{
              $location.url('/signin');
               }
            return;

        }
    };
});

我希望这有助于

我将创建一个服务来拦截请求,并添加一个带有令牌的头,如下所示:

angular.module('common').factory('xsrfTokenInterceptor', function(principal, $location) {
    return {
        'request': function(config) {
            //principal is the service that stores my user information and his token
            var identity = principal.identity();
            if (identity !== "undefined" && identity !== undefined && identity !== null) {
                var sessionToken = identity.session;

                if (sessionToken !== "undefined" && sessionToken !== undefined) {
                    config.headers['X-Csrf-Token'] = sessionToken;
                }
                return;
            } else{
              $location.url('/signin');
               }
            return;

        }
    };
});

我希望这有助于

使用角度拦截器此令牌专门用于此请求。因此,需要使用拦截器使用角度拦截器此令牌专门用于此请求。因此有必要使用拦截器