Javascript Google日历api不起作用

时间:2012-02-15 05:10:38

标签: javascript google-calendar-api

在我的网站中,我想使用javascript api将事件添加到谷歌日历。我通过我的邮件更改了邮件。但我在火灾中遇到错误。

这是我的代码:

<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi">
 google.load("gdata", "1");
 google.setOnLoadCallback(getMyFeed);
</script>   
</head>
<body>
<script type="text/javascript">
 var myService;
 var feedUrl = "http://www.google.com/calendar/feeds/galtech.staffs@gmail.com/private/full";
 function logMeIn() {
   scope = "http://www.google.com/calendar/feeds/";
   var token = google.accounts.user.login(scope);
 }
 function setupMyService() {
   myService = new google.gdata.calendar.CalendarService('exampleCo-exampleApp-1');
   logMeIn();
 }
 function getMyFeed() {
   setupMyService();

 myService.getEventsFeed(feedUrl, handleMyFeed, handleError);
 }

 function handleMyFeed(myResultsFeedRoot) {
   alert("This feed's title is: " + myResultsFeedRoot.feed.getTitle().getText());
 }

 function handleError(e) {
   alert("There was an error!");
   alert(e.cause ? e.cause.statusText : e.message);
 }

// Create the calendar service object
var calendarService = new google.gdata.calendar.CalendarService('GoogleInc-jsguide-1.0');

// The default "private/full" feed is used to insert event to the
// primary calendar of the authenticated user
var feedUri = 'https://www.google.com/calendar/feeds/default/private/full';

// Create an instance of CalendarEventEntry representing the new event
var entry = new google.gdata.calendar.CalendarEventEntry();

// Set the title of the event
entry.setTitle(google.gdata.Text.create('JS-Client: insert event'));

// Create a When object that will be attached to the event
var when = new google.gdata.When();

// Set the start and end time of the When object
var startTime = google.gdata.DateTime.fromIso8601("2008-02-10T09:00:00.000-08:00");
var endTime = google.gdata.DateTime.fromIso8601("2008-02-10T10:00:00.000-08:00");
when.setStartTime(startTime);
when.setEndTime(endTime);

// Add the When object to the event
entry.addTime(when);

// The callback method that will be called after a successful insertion from insertEntry()
var callback = function(result) {
  PRINT('event created!');
}

// Error handler will be invoked if there is an error from insertEntry()
var handleError = function(error) {
  PRINT(error);
}

// Submit the request using the calendar service object
calendarService.insertEntry(feedUri, entry, callback, handleError, google.gdata.calendar.CalendarEventEntry);
</script>
 <form>
 <input type="button" value="Start" onClick="getMyFeed()" />
 </form>
</body>
</html>

当我点击strat按钮时出现错误:

google.gdata is undefined   

...Service = new google.gdata.calendar.CalendarService('exampleCo-exampleApp-1'...

我的代码中有什么问题?

2 个答案:

答案 0 :(得分:1)

使用window.onload事件包装所有javascript代码, 我认为它可以帮助您摆脱“google.gdata未定义”错误。

答案 1 :(得分:-1)

google.gdata存在之前,您正在呼叫它。阅读代码,看起来你正在动态加载gdata库。看起来getMyFeed在加载时被调用。因此,在google.gdata执行完毕之前,请勿尝试访问getMyFeed

我还建议您将内联脚本移到您的头上,特别是在另一个script标记之上。