[英]FullCalendar.io not displaying events fed from JSON
我嘗試了多種方法來提供日歷中的events
。 我嘗試將 JSON 數據放入變量中並執行events = result
,並且我已經嘗試過 AJAX 就像我現在一樣。 數據是從 php function 獲取的,它是正確的語法,我控制台記錄數據,這是返回的內容: {title: 1, start: "2020-07-23"}
。 所以我不確定為什么會這樣。
$('#calendar').fullCalendar({
events:
{
url: '/modules/ajax/ajax_handler.php',
method: 'POST',
data: data,
success: function(response) {
console.log(response);
},
failure: function() {
alert('there was an error while fetching events!');
},
}
});
Ajax 處理程序:
elseif($_POST['action'] == 'getPeopleCountOnDate') {
$date = $_POST['date'];
$count = getPeopleCountOnDate($connection, $date);
echo $count;
}
PHP 腳本
function getBookingEventInfo($connection) {
$dates;
$query = "SELECT reservation_date, count(*) FROM bookings GROUP BY reservation_date";
if($stmt = $connection->prepare($query)){
$stmt->execute();
$stmt->bind_result($date, $count);
while($stmt->fetch()){
$dates = array(
"title" => $count,
"start" => formatDate($date, 14)
);
}
$stmt->close();
}
return json_encode($dates);
}
如果response
僅包含{title: 1, start: "2020-07-23"}
正如您在問題中提到的那樣,那么您遇到了問題,因為它不是數組。
FullCalendar 需要一系列事件,而不是單個 object。 即使該數組只包含 1 個事件,它也必須是一個數組。 您的服務器需要返回[{title: 1, start: "2020-07-23"}]
作為 JSON 才能正常工作。
要在您的 PHP 代碼中實現這一點,您可以這樣編寫:
function getBookingEventInfo($connection) {
$dates = array(); //declare an array to contain all the results
$query = "SELECT reservation_date, count(*) FROM bookings GROUP BY reservation_date";
if($stmt = $connection->prepare($query)){
$stmt->execute();
$stmt->bind_result($date, $count);
while($stmt->fetch()){
//push the query result into the main array
$dates[] = array(
"title" => $count,
"start" => formatDate($date, 14)
);
}
$stmt->close();
}
return json_encode($dates);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.