Is there a way to enable CORS on Github pages?

后端 未结 3 852
南方客
南方客 2020-12-31 05:12

I\'m hosting some JSON files on Github pages, but I am unable to use $.getJSON to retrieve them unless they come from the exact same domain.

Is there a

相关标签:
3条回答
  • 2020-12-31 05:50

    As a workaround one can use JSONP, but it's still slightly a pain because the files are static, and each one will need a unique callback method.

    Another alternative would be to store the files in Gists and use the Github API which works with CORS.

    A third possibility is to not store the files on Github pages and instead host them on an S3/Cloudfront distribution with CORS enabled.

    0 讨论(0)
  • 2020-12-31 05:54

    It's possible with the API, but it has a limit...

    "For requests using Basic Authentication or OAuth, you can make up to 5,000 requests per hour. For unauthenticated requests, the rate limit allows you to make up to 60 requests per hour. Unauthenticated requests are associated with your IP address, and not the user making requests."

    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'https://api.github.com/repos/jimmywarting/diezyweb/contents/index.html?ref=gh-pages');
    xhr.setRequestHeader("Accept", "application/vnd.github.3.raw");
    xhr.send();
    
    xhr.onload = function(e){
       alert(xhr.response)
    }

    0 讨论(0)
  • 2020-12-31 05:58

    Github Pages now has CORS enabled.

    The CORS header:

    Access-Control-Allow-Origin: *
    

    Is added by default on all responses from Github pages!

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