简体   繁体   中英

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 :

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. So, inject and use Base64 to encode the username and password

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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