请教一个跨域的问题: No 'Access-Control-Allow-Origin' header ,参考了很多教程还是解决不了。

2017-03-27 20:52:29 +08:00
 kmdd33
参考的教程如下: http://serverfault.com/questions/162429/how-do-i-add-access-control-allow-origin-in-nginxhttp://www.tuicool.com/articles/3aaQB3bhttps://segmentfault.com/q/1010000003116113

自己尝试在 nginx.conf 文件中添加了
location / {
add_header Access-Control-Allow-Origin *;
}





location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
# Om nom nom cookies
#
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# Tell client that this pre-flight info is valid for 20 days
#
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' '*';
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' '*';
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';
}
}

丝毫不起作用,求助后端伙伴们,怎么搞?
console 里面的错误(不知道如何贴图,就只能贴 error 了)如下:

XMLHttpRequest cannot load https://www.mydomain.com/?/ac... No 'Access-Control-Allow-Origin' header is present on the https://www.mydomain.com/?/ac... requested resource. Origin 'https://www.mydomain.com' is therefore not allowed access.
27071 次点击
所在节点    问与答
43 条回复
ihuotui
2017-03-27 20:56:26 +08:00
cross.org 有所有跨域知识
kmdd33
2017-03-27 21:00:42 +08:00
ferrum
2017-03-27 21:01:19 +08:00
别上来就贴一大堆格式乱了的配置啊,起码得说具体点吧。
fanwei
2017-03-27 21:05:10 +08:00
确实应该说清楚环境。比如油猴脚本里如果用 jquery 怎么设置都会有上面这个提示。必须用对应的 GM 函数。
kmdd33
2017-03-27 21:08:25 +08:00
环境是:军哥的 lnmp ,目前在输入服务器 ip 状态下用户可以登陆,@ferrum @ferrum @fanwei @ihuotui
kmdd33
2017-03-27 21:35:45 +08:00
自己用 Cloudflare 做了 301 跳转,无论是输入 mydomain.com 或者 www.domain.com 都会跳转到 https://www.mydomain.com
johnny23
2017-03-27 21:37:34 +08:00
我以前遇到这些问题 一怒之下把所有接口都做成 jsonp
kmdd33
2017-03-27 22:49:38 +08:00
@johnny23 如何用 jsonp
johnny23
2017-03-27 22:57:52 +08:00
@kmdd33 额 网上教程一大堆 唯一区别就是返回字符串是一个带了 callback 函数的字符串而已
ikaros
2017-03-27 23:23:27 +08:00
我记得设置 * 好像是没用的, console 里的提示记得就是说当前 host 和*不匹配之类的, 你是真的需求让所有外部都可以访问? 我当时因为只有两个 host 需要做这个,于是就把这两个 host 存在一个 list 里面,然后看来访的 host 是不是在这个 list 里面,在的话就把这个 host 加到 header 里面
Numbcoder
2017-03-27 23:37:00 +08:00

add_header 'Access-Control-Allow-Origin' '*';
改成
add_header Access-Control-Allow-Origin $http_origin;

试试
des
2017-03-27 23:41:52 +08:00
@ikaros 不知道是不是和地址有关, 127.0.0.1 是可以的

kmdd33
2017-03-28 04:00:57 +08:00
@ikaros 请问具体你是如何添加的呢?如果把*换成 https://www.mydomain.com. 放在自己的 Nginx.conf 文件里面,还是不行啊
kmdd33
2017-03-28 04:02:48 +08:00
@Numbcoder 按照你的方法,依然有错误
Showfom
2017-03-28 04:55:55 +08:00
你没指定文件的后缀 我们的配置给你参考

# Cross domain webfont access
location = /favicon.ico { log_not_found off; access_log off; }

location = /robots.txt { log_not_found off; access_log off; allow all; }

location ~* .(ogg|ogv|mp4|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
add_header "Access-Control-Allow-Origin" "*";
add_header "Timing-Allow-Origin" "*";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
expires max;
log_not_found off;
}

location ~* .(ttf|ttc|otf|eot|woff|woff2|svg|svgz)$ {
add_header "Access-Control-Allow-Origin" "*";
add_header "Timing-Allow-Origin" "*";
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
expires max;
log_not_found off;
}
Showfom
2017-03-28 04:56:34 +08:00
preload 那段头部不是 https 可以忽略
kmdd33
2017-03-28 05:25:08 +08:00
@Showfom 是不是把你发的这段代码,也粘贴到我服务器上 Nginx.conf 文件里面?
binux
2017-03-28 05:29:24 +08:00
把返回头发出来啊, console 没有用
Showfom
2017-03-28 06:23:55 +08:00
@kmdd33 嗯 用在 server 段
ikaros
2017-03-28 08:50:16 +08:00
@kmdd33 我不是配置在 nginx 里面的,我是在程序 handler 里面加的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/350723

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX