gpt4 book ai didi

angular - http拦截器不添加带有字符串标记 Angular header

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:53 26 4
gpt4 key购买 nike

我试图在每个请求的 header 中发送一个 token 。首先,我尝试做更复杂的事情,比如保存在本地存储中,但现在,我在这里尝试添加一个带有随机标记字符串的该死的 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 中

当我尝试使用 postman 或在浏览器中调用我的应用程序时,它不会添加 header 。为什么我做错了?

最佳答案

试试下面的代码,这是在授权 header 中发送/附加 token 的方式,然后在服务器上解码/检查这个 header 。

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

关于angular - http拦截器不添加带有字符串标记 Angular header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44540329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com