Load json from github file

10,201

Solution 1

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.

Solution 2

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.

Share:
10,201
rzs
Author by

rzs

Updated on July 04, 2022

Comments

  • rzs
    rzs almost 2 years

    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
    rzs about 9 years
    This did work, but as the other answer mentioned, it did end up returning the wrong mime type. Thank you though!
  • JamesWilson
    JamesWilson over 5 years
    Is this ?callback=? documented somewhere? How did you find out this even existed?
  • andyd_28
    andyd_28 over 5 years
    api.jquery.com/jquery.getjson details it very briefly under the sub heading JSONP
  • cursorrux
    cursorrux almost 3 years
    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.
  • Admin
    Admin almost 3 years
    Please add further details to expand on your answer, such as working code or documentation citations.