【发布时间】:2013-12-20 00:57:56
【问题描述】:
我在使用 AngularJS 和 Node ExpressJS 服务器时遇到了 Chrome CORS 问题。
我已经阅读了很多关于相同问题的帖子,并且我已经实施了解决方案,但仍然没有乐趣。
希望另一双眼睛能看到我哪里错了。
我正在开发本地盒子,正在开发中
From AngularJS app: http://localhost:3000
To Node ExpressJS Server : http://localhost:3001
Error is:
XMLHttpRequest cannot load http://localhost:3001/api/v1/articles.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
Node ExpressJS 配置是标准推荐:
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Methods', 'POST,GET,PUT,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
当从浏览器发帖时,这里是 Chrome 对服务器的飞行前请求:
请求标头:
OPTIONS /api/v1/articles HTTP/1.1
Host: localhost:3001
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://localhost:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
响应标头:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With
Access-Control-Allow-Methods:POST,GET,PUT,DELETE,OPTIONS
Access-Control-Allow-Origin:http://localhost:3000
Allow:GET
Connection:keep-alive
Content-Length:3
Content-Type:text/html; charset=utf-8
Date:Tue, 03 Dec 2013 19:24:22 GMT
POST 请求标头:
POST http://localhost:3001/api/v1/articles HTTP/1.1
Accept: application/json, text/plain, */*
Referer: http://localhost:3000/
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Content-Type: application/json;charset=UTF-8
然后出现控制台错误,没有post。
服务器配置、飞行前选项请求和访问控制标头对我来说都是正确的。
你能看出问题吗?
我不确定 OPTIONS 响应中的 Allow:GET 标头。
谢谢。
【问题讨论】:
-
您可以包含客户端代码的 sn-p 吗?你已经设置了
Access-Control-Allow-Credentials:true,你是否在你的 XMLHttpRequest 对象上设置了相应的xhr.withCredentials=true属性? -
这些是实际的
POST请求标头吗?如果是这样,为什么它包含一个完整的 URL 而不仅仅是一个路径(如OPTIONS请求)? -
@monsur 下面是一些相关代码: HTML:
-
和 Restangular 配置: angular.module('myApp') .config(function (RestangularProvider) { RestangularProvider.setBaseUrl('localhost:3001/api/v1'); RestangularProvider.setDefaultHttpFields({withCredentials: true}); } );
-
@robertklep .. 完整的 OPTIONS 标头为:请求 URL:localhost:3001/api/v1/rideshares 请求方法:OPTIONS 状态代码:200 OK 请求标头视图已解析 OPTIONS /api/v1/rideshares HTTP/1.1 主机:localhost:3001连接:保持活动访问控制请求方法:POST 来源:localhost:3000 用户代理:Mozilla/5.0(X11;Linux x86_64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/31.0.1650.57 Safari/537.36 访问-Control-Request-Headers:accept, content-type Accept:/ Referer:localhost:3000 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8