Cross-Origin Resource Sharing on GitHub Pages

后端 未结 5 1809
栀梦
栀梦 2020-12-08 04:33

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?

For example, can

相关标签:
5条回答
  • 2020-12-08 05:09

    You can use a CORS proxy.
    http://cors.io/ worked for me.

    Normal request:

    $.getJSON('https://blockchain.info/stats?format=json',function(data){})
    

    Request with proxy (just prepend http://cors.io/? on the url)

    $.getJSON('http://cors.io/?https://blockchain.info/stats?format=json',function(data){})
    

    UPDATE: The API doc have been updated, you just need to prefix your url with https://cors.io/?.

    0 讨论(0)
  • 2020-12-08 05:09

    FYI it looks like GitHub Pages now support CORS (at least in some situations). In this case custom domains with bare URLs (no www or github sub domain). This means using an A record and avoiding their caching CDN.

    When I go to enable-cors.org now I see the Access-Control-Allow-Origin: * header returned on all resources (from the network tab of the browser developer tools). In both Chrome and Firefox.

    I use this at https://isthetubeonstrike.com to access a JSON file cross domain from a mobile web app. The SSL/TLS is provided by going through CloudFlare BTW.

    0 讨论(0)
  • 2020-12-08 05:20

    What I'm getting from having put in a support ticket a few days ago is that CORS requests to GitHub Pages are perfectly fine.

    Getting content from another page, which is what the original post seems to be asking about, means that the other page's server has to have CORS requests set up or otherwise it will block your requests. Typically sites have public APIs to work around this issue if there is a real need to get content from them (e.g., MediaWiki for Wikipedia).

    0 讨论(0)
  • 2020-12-08 05:21

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

    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.

    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 :)


    Update

    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
    
    0 讨论(0)
  • 2020-12-08 05:25

    In my case, I was using a custom domain but I forgot to add the domain while deploying(ng deploy --base-href https://customdomain.com/). Check the network tab in dev-tools and observe the URL to check if it is generating the expected URL or not.

    0 讨论(0)
提交回复
热议问题