本文介绍了Angularjs设置授权头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想在我的请求中放置一个授权头,但它不工作。

I am trying to put an authorization header in my requests but it doesn't work.

我使用这个:

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

我也尝试过:

$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

我需要这样的东西:

Authorization: token

但我得到了:

Access-Control-Request-Headers:accept, authorization

放置令牌值。

我使用expressjs作为后端,我将其用于CORS:

I am using expressjs for the back-end and I using this for 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();
  });

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

Also say that testing with the chrome extension Advance Rest Client it is working fine. In the request header there is a Authorization: valueOfToken..

非常感谢。

推荐答案

授权不能简单。

指定weather a Basic $ c>承载授权

specify weather its a Basic or Bearer Authorization

类似

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

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

这篇关于Angularjs设置授权头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:05