AngularJS: 拒绝设置不安全的标头"Access-Control-Request-Headers"。

13

我正在尝试使用AngularJS调用本地运行的REST API。这是AngularJS代码:

$http.defaults.headers.common = {"Access-Control-Request-Headers": "accept, origin, authorization"};

$http.defaults.headers.common['Authorization'] = 'Basic amF5M2RlYzpqYXk=';
$http({method: 'GET', url: 'http://127.0.0.1:5000/user/jay3dec'}).
        success(function(data, status, headers, config) {

        }).
        error(function(data, status, headers, config) {
            alert(data);

        });

但是我在浏览器控制台中遇到了错误:

Refused to set unsafe header "Access-Control-Request-Headers" 

我尝试使用CURL查询运行在http://127.0.0.1:5000/user/jay3dec的REST API。

curl -H "Origin: http://127.0.0.1:8000" -H "Authorization: Basic amF5M2RlYzpqYXk=" http://127.0.0.1:5000/user/jay3dec --verbose

并且它给出了以下输出:

> GET /user/jay3dec HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
> Origin: http://127.0.0.1:8000
> Authorization: Basic amF5M2RlYzpqYXk=
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Content-Length: 454
< ETag: bff7b7db33baedb612276861e84faa8f7988efb1
< Last-Modified: Tue, 30 Dec 2014 14:32:31 GMT
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Authorization
< Access-Control-Allow-Methods: HEAD, OPTIONS, GET
< Access-Control-Allow-Max-Age: 21600
< Server: Eve/0.4 Werkzeug/0.9.6 Python/2.7.6
< Date: Sun, 25 Jan 2015 20:00:29 GMT
< 
* Closing connection 0
{"username": "jay3dec", "_updated": "Tue, 30 Dec 2014 14:32:31 GMT", "password": "jay", "firstname": "jay", "lastname": "raj", "phone": "9895590754", "_links": {"self": {"href": "/user/54a2b77f691d721ee170579d", "title": "User"}, "parent": {"href": "", "title": "home"}, "collection": {"href": "/user", "title": "user"}}, "_created": "Tue, 30 Dec 2014 14:32:31 GMT", "_id": "54a2b77f691d721ee170579d", "_etag": "bff7b7db33baedb612276861e84faa8f7988efb1"}

有人能发现可能的问题吗??

3个回答

9

$http.defaults.headers.common的代码是:

var xhr = createXhr();

xhr.open(method, url, true);
  forEach(headers, function(value, key) {
    if (isDefined(value)) {
        xhr.setRequestHeader(key, value);
    }
  });

...

function createXhr() {
    return new window.XMLHttpRequest();
}

关于XMLHttpRequest规范的引用,如果请求头与以下任一标头不区分大小写,则浏览器将终止:

Accept-Charset
Accept-Encoding
Access-Control-Request-Headers
Access-Control-Request-Method
Connection
Content-Length
...

这就是为什么你不能使用$http.defaults.headers.common来设置 Access-Control-Request-Headers头。浏览器会代替处理请求头信息。

4
问题出在CORS上。 您应该配置服务器端以允许Authorization在标头中。我不知道您使用的是什么服务器,但对于我的asp.net web api 2服务器,它看起来像:
Web.config
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
       <add name="Access-Control-Allow-Headers" value="Content-Type,Authorization" />
       <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
     </customHeaders>
   </httpProtocol>
 ......

0

出于安全考虑,浏览器不允许 head 设置一些安全风险,例如cookiehostreferer等。因此,不要使用浏览器来解析头部信息。可以在服务器端设置代理发送。

参考:跨域资源共享

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接