AngularJS:拒绝设置不安全的标头"Access-Control-Request-Headers" [英] AngularJS: Refused to set unsafe header "Access-Control-Request-Headers"

查看:252
本文介绍了AngularJS:拒绝设置不安全的标头"Access-Control-Request-Headers"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to call a REST API running locally using AngularJS. Here is the AngularJS code :

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

        });

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

But I'm getting an errors in the browser console :

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

我尝试查询运行在 http://127.0.0.1:5000/user/上的REST APIjay3dec 使用CURL.

I tried to query call the REST API running at http://127.0.0.1:5000/user/jay3dec using CURL.

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

任何一个地方都可以出什么问题吗?

Can any one spot what may be the issue ??

推荐答案

$ 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规范,浏览器将如果标头与以下标头之一不区分大小写,则终止

Referring to XMLHttpRequest specification , browser will terminate if header is a case-insensitive match for one of the following headers

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

这就是为什么您不能使用 $ http.defaults.headers.common 设置 Access-Control-Request-Headers 标头的原因.浏览器将代您处理请求标头.

That's why you can't use $http.defaults.headers.common to set Access-Control-Request-Headers header. Browser will handle request headers for you instead.

这篇关于AngularJS:拒绝设置不安全的标头"Access-Control-Request-Headers"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆