2017-06-14 27 views
1

我试图在每个请求的标头中发送一个标记。首先,我试图做更复杂的事情,比如保存在本地存储中,但现在,我试图添加一个带有随机标记字符串的该死的头文件,但它仍然没有添加头文件。为什么?这是app.js,我让这里只有这个:http拦截器不会添加包含字符串标记的标头角

'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

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

回答

0

尝试beow代码&这是发送/追加令牌授权头&然后解码/检查服务器这个头的方式。

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

好吧,仍然没有。当试图与邮递员没有任何变化,我不知道当我从浏览器发送它应该看什么。在$ _SERVER中? – IleNea

+0

你可以粘贴你的代码还是你有包含所有文件 – rroxysam

+0

什么代码?这是我用于这部分的所有代码。我不想使用持票人,但是能否请你先解释一下不正确的事情? – IleNea