简体   繁体   English

angular $ http标头不起作用

[英]angular $http headers not working

My client side look like this : 我的客户端看起来像这样:

$http(
       {
            method: 'GET',
            url: myConfig.serverUrl + '/getUserId',
            data: '',
            headers: { 'Authorization' : '21321313'},
       });

Server side in Node js and express : Node js中的服务器端并表示:

app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
next();

}); });

But request not set the Authorization value 但请求不设置授权值
OPTIONS /getUserId HTTP/1.1. Host: xxxxx. Connection: keep-alive. Pragma: no-cache. Cache-Control: no-cache. Access-Control-Request-Method: GET. Origin: http://localhost:9000. User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36. Access-Control-Request-Headers: authorization. Accept: */*. Referer: http://localhost:9000/. Accept-Encoding: gzip, deflate, sdch. Accept-Language: en-US,en;q=0.8,he;q=0.6.
I have try many ways but none of them work. 我尝试了很多方法,但没有一种方法可行。 When i try to to that with postman i get the value so i think my problem in my angular side but i cant figure out what is the problem . 当我尝试与邮递员,我得到的价值,所以我认为我的角度方面的问题,但我不知道是什么问题。

在服务器端这个问题,在“OPTIONS”API调用中无需检查授权

您需要指定authorization type ,例如'Authorization', 'Basic ZWx1c3VhcmlvOnlsYWNsYXZl'

In Basic authentication, base64 are used to decode the username and password. 在基本身份验证中,base64用于解码用户名和密码。 So, inject and use Base64 to encode the username and password 因此,注入并使用Base64来编码用户名和密码

var token = Base64.encode(username + ':' + password);
$http.defaults.headers.common['Authorization'] = 'Basic ' + token;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM