GitHub 页面上的跨域资源共享 [英] Cross-Origin Resource Sharing on GitHub Pages

查看:23
本文介绍了GitHub 页面上的跨域资源共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法为 GitHub Pages 上托管的静态页面启用跨域资源共享 (CORS)在 Javascript 中允许跨域请求?

Is there a way to enable Cross-Origin Resource Sharing (CORS) for a static page hosted on GitHub Pages to allow cross-origin requests in Javascript?

例如,我们能否以某种方式指示 GH Pages 添加这些 HTTP 响应标头:

For example, can we instruct GH Pages somehow to add these HTTP response headers:

Access-Control-Allow-Origin:*  
Access-Control-Allow-Methods:GET,POST
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers:*

在他们的 文档 中找不到任何内容,而这个 ...

Couldn't find anything in their documentation, and this ...

...GitHub Pages 不支持客户服务器配置文件比如 .htaccess 或者 .conf...

...GitHub Pages does not support customer server configuration files such as .htaccess or .conf...

...听起来不太有希望 - 或者有什么办法吗?

... doesn't sound very promising - or is there a way?

推荐答案

耶!看起来 GitHub Pages 现在支持 CORS:https://twitter.com/invisiblecomma/status/575219895308324864

Yay! Looks like GitHub Pages now supports CORS: https://twitter.com/invisiblecomma/status/575219895308324864

这可以通过将请求发送到 enable-cors.org(托管在 GitHub Pages 上)来验证.运行这个命令:curl -v enable-cors.org >/dev/null 返回一个 Access-Control-Allow-Origin: * 标头.

This can be verified by curling a request to enable-cors.org (which is hosted on GitHub Pages). Running this command: curl -v enable-cors.org > /dev/null returns an Access-Control-Allow-Origin: * header.

在 GitHub 页面上无法支持 CORS,但我很想看到这个功能.我们在 GitHub 页面上托管 http://enable-cors.org,我们无法在网站上启用 CORS本身:)

There's no way to support CORS on GitHub Pages, though I'd love to see this feature. We host http://enable-cors.org on GitHub Pages, and we can't enable CORS on the site itself :)

正如@Styx 所指出的,GitHub 页面现在总是重定向到 HTTPS.因此,如果您想自己确认所有来源都是允许的,对于使用 GitHub 页面的特定站点,请尝试使用 -Lcurl (以遵循所涉及的重定向).例如:

As noted by @Styx GitHub Pages now always redirect to HTTPS. So if you want to confirm for yourself that all origins are allowed, for a particular site using GitHub pages, try curl with -L (to follow the redirects that are involved). E.g.:

$ curl -vL square.github.io/okhttp 2>&1 | fgrep -i access-control-allow-origin

这篇关于GitHub 页面上的跨域资源共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆