我已经将我的 FullCalendar 配置为从 AJAX 请求中提取其事件,但当页面首次加载时,它们不会呈现在日历上。
$(document).ready(function() {
sh1client = new Array();
sh2client = new Array();
$('#sh1_cal').fullCalendar({
height: 1000,
minTime:'9:00am',
maxTime:'5:00pm',
editable: false,
events: function(start, end, callback) {
$.ajax({
type: 'GET',
url: 'http://localhost:8080/getEvents',
dataType: 'json',
success: function(reply) {
console.log(reply.first);
callback(reply.first);
}
});
}
});
$("#sh1_cal").fullCalendar('addEventSource', sh1client); // these are the clientside arrays
});
在服务器上,
app.get('/getEvents', function(req, res){
console.log('Server: passing events...');
var arrays = {first: sh1, second: sh2}
var pack = JSON.stringify(arrays)
res.writeHead(200, {'Access-Control-Allow-Origin' : '*', 'Content-Type': 'application/json'});
res.end(pack);
});
这些事件最初不会加载有什么原因吗?一切似乎都很好,但就像回调不起作用或其他什么。
编辑:这是我尝试过的另一种方法
events: {
url: 'http://localhost:8080/getEvents',
type: 'GET',
error: function() {
alert('there was an error while fetching events!');
},
success: function(reply) {
console.log(reply);
//callback(reply.first);
},
color: 'yellow', // a non-ajax option
textColor: 'black' // a non-ajax option
}
编辑:JavaScript控制台将其显示为在加载后立即发布到页面(这是数组中的第一个对象:
[Object]
allDay: "false"
end: "1392129000"
id: "phil@google.com"
room: "Sh1"
start: "1392127200"
title: "Phil - Google"
__proto__: Object
length: 1
__proto__: Array[0]
你试过使用全日历吗?
http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/
Fullcalendar 默认数据类型为 JSON,缓存默认为 false。
将您的一些代码与文档中的代码相结合:
$('#calendar').fullCalendar({
events: {
url: 'http://localhost:8080/getEvents',
type: 'GET',
error: function() {
alert('there was an error while fetching events!');
},
success: function(reply) {
console.log(reply.first);
callback(reply.first);
},
color: 'yellow', // a non-ajax option
textColor: 'black' // a non-ajax option
}
});
您可以尝试剪切和粘贴JSON字符串,看看是否可以在没有ajax调用
的情况下进行渲染
events: [
{
end: 1392129000,
id: "phil@google.com",
room: "Sh1",
start: 1392127200,
title: "Phil - Google"
}]
您还可以处理响应:
$('#myCalendar').fullCalendar({
...
eventSources : [{
url: 'myUrl',
type: 'GET',
},
success: function(replyObject) {
var results = [];
var reply= replyObject.Results[0];
for(var index in reply) {
results.push(reply[index]);
}
return results;
}
}]
...