3

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!

rzs
  • 99
  • 1
  • 3
  • 8
  • I am not sure you have any options other than to use a server side script and CURL the json page and re-serve it on your local URL. Not sure if that is even possible with your hosting. – John Fable Apr 13 '15 at 19:14
  • It's better to have real repository url to help. – David Jacquel Apr 13 '15 at 21:35

3 Answers3

5

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.

David Jacquel
  • 51,670
  • 6
  • 121
  • 147
4

Try adding ?callback=? to force it to use jsonp:

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

It appears to get a response, but in this case it is a 400 bad request so I cannot confirm its success.

andyd_28
  • 161
  • 6
1

I found a simple solution. If you add this to your URL, it's works: https://cors-anywhere.herokuapp.com/

Example: https://cors-anywhere.herokuapp.com/https://raw.githubusercontent.com/mydata.json

Lucas Moy
  • 11
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – cursorrux Sep 05 '21 at 11:05
  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 05 '21 at 11:05