Angularjs 设置授权头 [英] Angularjs set a authorization header
本文介绍了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
我需要这样的东西:
Authorization: token
但我明白了:
Access-Control-Request-Headers:accept, authorization
那么,我在任何地方都没有令牌值.
Then, I don't have in any place the token value.
我在后端使用 expressjs,我在 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 扩展 Advance Rest Client 测试它工作正常.在请求头中有一个 Authorization: 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..
非常感谢.
推荐答案
授权不能简单.
指定天气是Basic
或Bearer
授权
类似的东西
$http.defaults.headers.common['Authorization'] = 'Basic ' + token;
或
$http.defaults.headers.common['Authorization'] = 'Bearer ' + token;
这篇关于Angularjs 设置授权头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文