Load json from github file

China☆狼群 提交于 2020-01-16 04:20:08

问题


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!


回答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.




回答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.



来源:https://stackoverflow.com/questions/29612800/load-json-from-github-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!