简体   繁体   English

Angularjs 设置授权头

[英]Angularjs set a authorization header

I am trying to put an authorization header in my requests but it doesn't work.我试图在我的请求中放置一个授权标头,但它不起作用。

I am using this:我正在使用这个:

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

And also I tried this:我也试过这个:

$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.然后,我在任何地方都没有令牌值。

I am using expressjs for the back-end and I using this for CORS:我在后端使用 expressjs,我在 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();
  });

Also say that testing with the chrome extension Advance Rest Client it is working fine.还说使用 chrome 扩展 Advance Rest Client 进行测试它工作正常。 In the request header there is a Authorization: valueOfToken..在请求头中有一个 Authorization: valueOfToken..

Thanks a lot.非常感谢。

Authorization can't be plain.授权不能简单明了。

specify weather its a Basic or Bearer Authorization指定天气其BasicBearer授权

Something like就像是

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

or或者

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

I was having this same issue and it turned out the issue had to do with Apache configuration on the server side.我遇到了同样的问题,结果证明该问题与服务器端的 Apache 配置有关。 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($httpProvider) {
    $httpProvider.defaults.common['Authorization'] = token;
    // For angular 1.5, use:  
    // $httpProvider.defaults.headers.common['Authorization'] = token;        
});

Try this:尝试这个:

$http({

url : "127.0.0.1/login",
method : 'GET',
headers : {
      Content-Type : 'application/json',    
      Authorization: token
      }
}).success(function(data){
    alert("login Successfully");
}).error(function(error){
    alert("login error");
})

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

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