Avihay m
Avihay m

Reputation: 551

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 .

Upvotes: 3

Views: 579

Answers (3)

digit
digit

Reputation: 4565

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;

Upvotes: 0

Nadeer Ahammed
Nadeer Ahammed

Reputation: 100

This issue in server side, In "OPTIONS" API call no need to check Authorization

Upvotes: 2

Đào Minh Hạt
Đào Minh Hạt

Reputation: 2930

You need to specify authorization type something like 'Authorization', 'Basic ZWx1c3VhcmlvOnlsYWNsYXZl'

Upvotes: 1

Related Questions