我正在使用FullCalendar在日历上显示员工工作时间。
我通过 ajax 调用拉取事件,如下所示:
"events": function(start, end, timezone, callback) {
//create the data to be sent
var objectToSend = {
"start_date": start.format("YYYY-MM-DD"),
"finish_date": end.format("YYYY-MM-DD"),
};
//craft and make the request
$.ajax({
url: 'calendar/test',
data: objectToSend,
type: 'POST',
cache: false
}).done(function(data) {
//on success call `callback` with the data
callback(data)
})
}
这工作得很好,但是我在我的控制台中显示一个错误"未捕获的类型错误:无法读取未定义的属性'hasTime'",并且这是来自fullcalendar.min.js:6
。
我对 JavaScript 不是很流利,但我的搜索表明我要么没有提供正确的日期,要么那里有垃圾数据。
据我所知,我提供了所有正确的数据。生成数据的函数如下所示:
public function test(Request $request) {
$start_date = Input::get('start_date');
$finish_date = Input::get('finish_date');
$shifts = Roster::whereBetween('date', array($start_date, $finish_date)) - > get();
foreach($shifts as $shift) {
$start = $shift - > date.
' '.$shift - > start_time;
$finish = $shift - > date.
' '.$shift - > finish_time;
$events[] = array(
'title' => $shift - > staff - > first_name,
'start' => Carbon::createFromFormat('Y-m-d H:i:s', $start) - > toDateTimeString(),
'end' => Carbon::createFromFormat('Y-m-d H:i:s', $finish) - > toDateTimeString(),
'id' => $shift - > id,
'allDay' => false
);
}
return json_encode($events);
}
其中输出:
[{"title":"Gemma","start":"2016-02-01 18:00:00","end":"2016-02-01 22:00:00","id":1,"allDay":false},
{"title":"Gemma","start":"2016-01-26 18:00:00","end":"2016-01-26 22:00:00","id":49,"allDay":false}]
谁能发现我做错了什么?我只是想用它来渲染给定月份的事件。
编辑:控制台输出.log(数据)
它打印出:
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
打开这个,我得到:
0: Object
打开这个,我得到:
allDay: false
end: "2016-02-01 22:00:00"
id: 1
start: "2016-02-01 18:00:00"
title: "Gemma"
似乎是你给了fullCalendar
错误的事件参数,
尝试先呈现一些手动事件。
events: [{
title: 'event1',
start: '2010-01-01'
}, {
title: 'event2',
start: '2010-01-05',
end: '2010-01-07'
}, {
title: 'event3',
start: '2010-01-09T12:30:00',
allDay: false // will make the time show
}]
之后,确保您的事件与 fullCalendar 预期的参数匹配。
我无法弄清楚上面的代码出了什么问题,但是我通过使用 JSON 提要来绕过它:
events: {
url: 'calendar/test',
error: function()
{
alert("error");
},
success: function()
{
console.log("successfully loaded");
}
}
我得到了这个错误,具有以下函数
var postToServerAjax = function(event, delta, revertFunc)
{
$.post('/url', {event: event}, function(data)
{
}, 'json').fail(function()
{
revertFunc();
alert('We got an error.');
});
};
我发现这是因为我试图将事件传递给 post() 函数。我是否更改名称并不重要,一旦我传递它,我认为它试图序列化它,并导致该错误。现在我手动指定和反对,在那里我"克隆"相关的 id,重新开始和结束,因为无论如何我都不需要其他任何东西。
use: JSON.parse(data)
in reponse ajax.