Load json from github file
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.
rzs
Updated on July 04, 2022Comments
-
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 domainhttp://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 about 9 yearsThis did work, but as the other answer mentioned, it did end up returning the wrong mime type. Thank you though!
-
JamesWilson over 5 yearsIs this
?callback=?
documented somewhere? How did you find out this even existed? -
andyd_28 over 5 yearsapi.jquery.com/jquery.getjson details it very briefly under the sub heading JSONP
-
cursorrux almost 3 yearsWhile 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 almost 3 yearsPlease add further details to expand on your answer, such as working code or documentation citations.