0

我已将我的谷歌日历公开,我正在尝试使用该ics文件,但我遇到了问题XMLHttpRequest

到目前为止我已经尝试过:

如果我使用我的 googleCalendarId 和 googleCalendarApiKey,它会起作用:

   $('#calendar').fullCalendar({
        googleCalendarApiKey: '*************myApiKey**************',

        events: {
          googleCalendarId: 'chris.beckett@schoolspider.co.uk'
        },

        eventClick: function(event) {
            console.log(event.start);
            console.log(event.end);
            return false;
        }, 

        loading: function(bool) {
            $('#loading').toggle(bool);
        }
    });

然后,当我尝试像这样使用实际的 ics 文件时:

   $('#calendar').fullCalendar({        
        events: {
          url: 'https://calendar.google.com/calendar/ical/chris.beckett%40schoolspider.co.uk/public/basic.ics'
        },

        eventClick: function(event) {
            console.log(event.start);
            console.log(event.end);
            return false;
        }, 

        loading: function(bool) {
            $('#loading').toggle(bool);
        }
    });

它在控制台日志中显示以下错误:

XMLHttpRequest 无法加载 https://calendar.google.com/calendar/ical/chris.beckett%40schoolspider.co.uk/public/basic.ics。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“ http://127.0.0.1:8887 ”。

我还尝试设置以下内容:

//htaccess file
Header set Access-Control-Allow-Origin "*"
//php 
header("Access-Control-Allow-Origin: *");
//xhr 
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
4

1 回答 1

0

通过使用这种方法,我设法将ICS文件保存到 fullcalendar -

function icsToArray($paramUrl) {
        $icsFile = file_get_contents($paramUrl);

        $icsData = explode("BEGIN:", $icsFile);

        foreach($icsData as $key => $value) {
            $icsDatesMeta[$key] = explode("\n", $value);
        }

        foreach($icsDatesMeta as $key => $value) {
            foreach($value as $subKey => $subValue) {
                if ($subValue != "") {
                    if ($key != 0 && $subKey == 0) {
                        $icsDates[$key]["BEGIN"] = $subValue;
                    } else {
                        $subValueArr = explode(":", $subValue, 2);
                        $icsDates[$key][$subValueArr[0]] = $subValueArr[1];
                    }
                }
            }
        }
        return $icsDates;
    }
于 2016-03-16T10:58:17.510 回答