Angularjs设置授权头 [英] Angularjs set a authorization header

查看:310
本文介绍了Angularjs设置授权头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把我的请求的授权头,但它不工作。

I am trying to put an authorization header in my requests but it doesn't work.

我使用这样的:

var config = {headers: {
  'Authorization': token
  }
};
return $http.get('http://localhost:3000/apis/users/all', config);

和还我尝试这样做:

$http.defaults.headers.common['Authorization'] = token;

但随着这两种情况下我得到这个消息头,后端的请求:

But with both cases I got this headers in the back-end request:

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:es-ES,es;q=0.8,ca;q=0.6,en;q=0.4,gl;q=0.2
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:3000
Origin:http://localhost:8000
Referer:http://localhost:8000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4)         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36

我需要的东西是这样的:

I need something like this:

Authorization: token

但我得到这样的:

But I got this:

Access-Control-Request-Headers:accept, authorization

然后,我并不在任何地方都有令牌值。

Then, I don't have in any place the token value.

我使用前pressjs的后端,我用这个为CORS:

I am using expressjs for the back-end and I using this for CORS:

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
    next();
  });

也说,使用Chrome扩展高级REST客户端,这是工作罚款测试。在请求头有一个授权:valueOfToken ..

Also say that testing with the chrome extension Advance Rest Client it is working fine. In the request header there is a Authorization: valueOfToken..

非常感谢。

推荐答案

我有同样的问题,它原来的问题曾与服务器端的Apache配置做。有一件事我确实发现可能对你有用这里是我的授权令牌是在 $ P $发送pflight 请求头,而不是主要的要求,所以它可能不会出现在头请求时,你看它的开发者工具。

I was having this same issue and it turned out the issue had to do with Apache configuration on the server side. One thing I did find that might be useful to you here is that my Authorization token is sent in the preflight request headers rather than the main request, so it might not appear to be in the headers of the request when you look at it in the developer tools.

我设置我的默认设置是这样的:

I'm setting my defaults like this:

app.config(function($http) {
    $httpProvider.defaults.common['Authorization'] = token;        
});

这篇关于Angularjs设置授权头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆