好的,我已经在这里和周围看了很多问题但似乎无法解决这个问题,所以如果这个问题在某种程度上是重复的并且我错过了一些东西请指出我正确的方向。
我在api.example.com上运行了一个Node / Sails API服务器,从www.example.com运行了我的js-sdk,在www.anotherdomain.com上运行了一个嵌入js-sdk的客户端
两者都是api。和www。在NGINX配置中为CORS提供此功能:
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
}
当我从anotherdomain.com运行帖子到有效的终端时,我收到此错误:
XMLHttpRequest cannot load http://api.example.com/users. The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin 'www.anotherdomain.com' is therefore not allowed access.
答案 0 :(得分:1)
如果您不介意在应用程序中执行此操作,可以通过添加config / cors.js直接在Sails中配置CORS:
Hash#fetch
有关选项的详细信息,请参阅文档http://devblog.avdi.org/2009/03/16/go-fetch/和there