我正在尝试使用 Angular(版本 1.0.5)webapp 中的应用程序密钥和令牌来锻炼 Trello API。服务器似乎正确配置为处理CORS。来自enable cors的带有http://test-cors.org的测试请求按预期工作。
当我在我的一个角度控制器中进行发布请求时:
$http.post(url).success(function(data) {
$scope.server_resp = data;
});
我得到一个请求标头字段 Content-Type is not allowed by Access-Control-Allow-Headers错误。(尽管如下所示,Access-Control-Allow-Origin 设置为“*”)。为什么要添加此标头并且可以将其删除?
XMLHttpRequest
当我使用原始 XMLHttpRequest 发出相同的请求时,它成功了。以下是 XMLHttpRequest 的标头:
Request Method:POST
Status Code:200 OK
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Host:api.trello.com
Origin:http://192.168.0.125:9000
Referer:http://192.168.0.125:9000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Response
Access-Control-Allow-Methods:GET, PUT, POST, DELETE
Access-Control-Allow-Origin:*
Cache-Control:max-age=0, must-revalidate, no-cache, no-store
Content-Length:563
Content-Type:application/json
Date:Mon, 18 Mar 2013 02:49:37 GMT
Expires:Thu, 01 Jan 1970 00:00:00
X-Powered-By:Express
X-Server-Time:1363574977568
角 $http.post
以下是角度发起请求的标头。请注意,浏览器发出了飞行前 OPTIONS 请求:
Request Method:OPTIONS
Status Code:200 OK
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, origin, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:api.trello.com
Origin:http://192.168.0.125:9000
Referer:http://192.168.0.125:9000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Response
Access-Control-Allow-Methods:GET, PUT, POST, DELETE
Access-Control-Allow-Origin:*
Content-Length:0
Content-Type:text/html; charset=utf-8
Date:Mon, 18 Mar 2013 02:51:00 GMT
X-Powered-By:Express
有没有办法配置 Angular 的请求标头以允许上面的 $http.post() 代码工作?