Search code examples
jqueryjsongitgithub-pages

Load json from github file


I'm trying to access/retrieve some json data from a webpage hosted by github pages, so all of my files are within my repository.

I'm trying to do

$.getJSON("https://raw.githubusercontent.com/mydata.json", function(data){
      console.log(data);
}

from my html page on github-pages, since that's the only way I know how to access the json data right now. However, The json content is on the domain https://raw.githubusercontent.com while my page is from the domain http://mygithub.github.io, so I'm getting a 'Access-Control-Allow-Origin' error, even though the data is all hosted on the same place at least?

Any ideas on how to fix that, or get around that? I've been looking up some CORS stuff but don't completely understand it, nor how to change anything related to that on github.

Thanks!


Solution

  • Why don't you simply call http://mygithub.github.io/mydata.json ?

    githubusercontent.com is slow and doesn't return the correct mime type for json text-plain instead of application/json.