有没有办法为 GitHub Pages 上托管的静态页面启用跨域资源共享 (CORS)允许 JavaScript 中的跨域请求?
例如,我们能否以某种方式指示 GH Pages 添加这些 HTTP 响应 header :
Access-Control-Allow-Origin:*
Access-Control-Allow-Methods:GET,POST
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers:*
在其 documentation 中找不到任何内容,还有这个...
...GitHub Pages does not support customer server configuration files such as .htaccess or .conf...
...听起来不太有希望 - 或者有办法吗?
最佳答案
编辑: 耶!看起来 GitHub Pages 现在支持 CORS:https://twitter.com/invisiblecomma/status/575219895308324864
这可以通过 curl 请求到enable-cors.org(托管在GitHub Pages上)来验证。运行此命令:curl -v enable-cors.org >/dev/null
返回 Access-Control-Allow-Origin: *
header 。
GitHub Pages 上无法支持 CORS,尽管我很想看到此功能。我们主持http://enable-cors.org在 GitHub Pages 上,我们无法在网站本身上启用 CORS :)
更新
正如 @Styx 所指出的,GitHub 页面现在始终重定向到 HTTPS。因此,如果您想亲自确认是否允许所有来源,对于使用 GitHub 页面的特定站点,请尝试使用 curl
和 -L
(遵循所涉及的重定向) 。例如:
$ curl -vL square.github.io/okhttp 2>&1 | fgrep -i access-control-allow-origin
关于github - GitHub Pages上的跨源资源共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26416727/