jquery - 谷歌日历 CORS 错误

标签 jquery json google-calendar-api

我正在尝试使用这段 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/

相关文章:

google-apps-script - 使用 Google Apps 脚本从基于 Google 表格的 Google 日历中删除事件

javascript - 如何在 jquery 中为每日、每周和每月的步骤创建范围 slider

javascript - 使用 jquery 获取引用 url 的一部分

javascript - 优化正则表达式搜索电话#搜索结果(JS)?

javascript - 将内容类型更改为 JSON 时,从数据库加载更多内容不起作用

python - Google 服务帐户无法模拟 GSuite 用户

javascript - 提交表格时保存签名

jQuery悬停()不适用于绝对定位的元素和动画

java - 反序列化的 JSON 对象的集合元素是 LinkedHashMap

calendar - 如何更新ICS日历 session ?