【问题标题】:http interceptor doesn't add header with string token angularhttp拦截器不添加带有字符串标记角的标头
【发布时间】:2017-06-14 09:09:00
【问题描述】:

我正在尝试在每个请求的标头中发送一个令牌。首先,我尝试做更复杂的事情,比如保存在本地存储中,但是现在,我尝试添加一个带有随机标记字符串的该死的标头,但它仍然没有添加标头。为什么?这是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 中

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

【问题讨论】:

    标签: angular http interceptor


    【解决方案1】:

    尝试下面的代码,这是在授权标头中发送/附加令牌然后在服务器上解码/检查此标头的方法。

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

    【讨论】:

    • 好吧,还是什么都没有。尝试使用邮递员时没有任何变化,我不确定从浏览器发送它时应该看哪里。在 $_SERVER 中?
    • 你能粘贴你的代码还是包含所有文件
    • 什么代码?这是我用于这部分的所有代码。而且我不想使用 Bearer,但你能先解释一下什么是不正确的吗?
    • 您是否包含了配置文件,因为正如您所说的没有打印任何内容,所以可能会发生并检查代码是否在请求块中运行。匿名打印任何东西。像 console.log("Hello");
    • 因为我认为,你的代码没有运行,否则它应该给出类似 undefined 的东西
    猜你喜欢
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多