http拦截器不会添加带有字符串标记角度的标头

时间:2017-06-14 09:09:00

标签: angular http interceptor

我正在尝试在每个请求的标头中发送一个令牌。首先,我尝试做更复杂的事情,比如保存在本地存储中,但是现在,我在这里尝试添加一个带有令牌字符串的该死的标头随机,它仍然没有添加标题。为什么?这是app.js,我只在这里说:

'use strict';

function testInterceptor() {
 return {
    request: function(config) {
        config.headers['x-token'] = 'lalalalala';
        console.log(config);
        console.log("Aaa");
        return config;
    }
  }
}

var app = angular.module('myapp', ['oc.lazyLoad', 'LocalStorageModule', 'oc.lazyLoad', 'ngRoute', 'naif.base64', 'ui.router', 'ngAnimate', 'ui.bootstrap']);

app.factory('testInterceptor', testInterceptor);
app.config(function($httpProvider) {
    $httpProvider.interceptors.push('testInterceptor');
});

在console.log中,它甚至不打印任何东西。我还评论了身份验证提供程序,并让:

Default:
   Anonymous: ~

在security.yml

当我尝试使用邮递员或在浏览器中调用我的应用时,它不会添加标题。我为什么做错了?

1 个答案:

答案 0 :(得分:0)

尝试beow code&这是在授权标题&中发送/附加令牌的方法。然后在服务器上解码/检查此标题。

function testInterceptor() {
 return {
    request: function(config) {
         config.headers.Authorization = 'Bearer ' + 'lalalalala';
         return config;
    }
  }
}
相关问题