Javascript Angularjs 设置授权头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32425107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 08:01:28  来源:igfitidea点击:

Angularjs set a authorization header

javascriptangularjsexpress

提问by PacoRampas

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. In the request header there is a Authorization: valueOfToken..

还说使用 chrome 扩展 Advance Rest Client 进行测试它工作正常。在请求头中有一个 Authorization: valueOfToken..

Thanks a lot.

非常感谢。

回答by maddygoround

Authorization can't be plain.

授权不能简单明了。

specify weather its a Basicor BearerAuthorization

指定天气其 aBasicBearer授权

Something like

就像是

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

or

或者

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

回答by kfhohn

I was having this same issue and it turned out the issue had to do with Apache configuration on the server side. One thing I did find that might be useful to you here is that my Authorization token is sent in the preflightrequest 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.

我遇到了同样的问题,结果证明该问题与服务器端的 Apache 配置有关。我确实发现在这里可能对您有用的一件事是,我的授权令牌是在预检请求标头中发送的,而不是在主请求中发送的,因此当您查看它时,它可能不会出现在请求的标头中开发者工具。

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;        
});

回答by Sandeep

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");
})