http 拦截器不添加带有字符串标记 angular 的 header
http interceptor doesn't add header with string token angular
我试图在 header 中针对每个请求发送令牌。首先,我尝试做更复杂的事情,比如保存在本地存储中,但现在,我在这里尝试添加一个带有随机标记字符串的该死的 header,但它仍然没有添加 header。为什么?就是这个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
当我尝试使用邮递员或在浏览器中调用我的应用程序时,它不会添加 header。为什么我做错了?
试试下面的代码,这是 send/append 授权令牌 header 然后 decode/check 服务器上的这个 header 的方法。
function testInterceptor() {
return {
request: function(config) {
config.headers.Authorization = 'Bearer ' + 'lalalalala';
return config;
}
}
}
我试图在 header 中针对每个请求发送令牌。首先,我尝试做更复杂的事情,比如保存在本地存储中,但现在,我在这里尝试添加一个带有随机标记字符串的该死的 header,但它仍然没有添加 header。为什么?就是这个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
当我尝试使用邮递员或在浏览器中调用我的应用程序时,它不会添加 header。为什么我做错了?
试试下面的代码,这是 send/append 授权令牌 header 然后 decode/check 服务器上的这个 header 的方法。
function testInterceptor() {
return {
request: function(config) {
config.headers.Authorization = 'Bearer ' + 'lalalalala';
return config;
}
}
}