我正在尝试使用这段 jquery 代码从 google 日历获取 json 数据:
var calendar_json_url = "http://www.google.com/calendar/feeds/some-@gmail.com/public/full?orderby=starttime&sortorder=ascending&max-results=3&futureevents=true&alt=json";
// Get list of upcoming events formatted in JSON
jQuery.getJSON(calendar_json_url, function(data){
// Parse and render each event
jQuery.each(data.feed.entry, function(i, item){
// Render the event
jQuery("#gcal-events li").last().after( "<li>" + item.title.$t + "</li>" );
});
});
但问题是我不断收到“跨源请求被阻止”错误。我不想使用默认的谷歌日历。我只需要数据并想以另一种方式重新格式化它! 难道我做错了什么? 当我添加 &callback=?错误不再出现,但也没有获取数据!
最佳答案
浏览器安全模型限制脚本访问从加载页面的域以外的域传递的数据,除非服务器启用跨源资源共享(请阅读 Spec ,或more readable discussion from Adobe)。
Google 似乎未在其 Feed 上启用 CORS 请求。据我所知,您必须注册为开发人员并使用 JavaScript API 发出跨源请求:https://developers.google.com/api-client-library/javascript/dev/dev_jscript#Option3UseCORS
关于jquery - 谷歌日历 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24846063/