Fullcalendar 没有加载我的 json 来显示事件
Fullcalendar isn't loading my json to show events
我一直在摆弄 fullcalendar java 脚本,我似乎无法让日历加载我的 mysql 数据库中的任何数据。
我的 db-connect.php 文件 returns 当我测试它时 table 中的第一个条目,但仍未填充到日历中。
<?php
// List of events
$events = array();
// connection to the database
$mysqli = new mysqli("localhost","username","password","database");
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//fetches and sends data in string
$result = $mysqli->query("SELECT * FROM events ORDER BY id");
$events = $result->fetch_array(MYSQLI_ASSOC);
echo json_encode($events);
?>
我还有一个 get_events.php,我尝试将其设置为仅提取日期范围。当我测试它时它不会提取任何信息,但这应该是因为我在没有日期范围的情况下测试它。
<?php
// List of events
$events = array();
$start = $_GET["start"];
$end = $_GET["end"];
// connection to the database
$mysqli = new mysqli("localhost","agaraapp_events","!QAZ@WSX3edc4rfv","agaraapp_events");
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//fetches and sends data in string
$query = mysqli_query($link, "SELECT * FROM events WHERE start >= '$start' AND end <= '$end'");
while($fetch = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
$e = array();
$e['id'] = $fetch['id'];
$e['title'] = $fetch['title'];
$e['start'] = $fetch['start'];
$e['end'] = $fetch['end'];
array_push($events, $e);
}
echo json_encode($events);
?>
那么这是我的日历代码。
<head>
<meta charset="utf-8" />
<link href="CalendarJava/main.css" rel="stylesheet" />
<script src="CalendarJava/main.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives',
initialView: 'dayGridMonth',
events: {
url: 'get_events.php',
type: 'POST',
data : {
},
success : function(response){
}
}
});
calendar.render();
});
</script>
</head>
我已经将 url 更改为 get_events.php 和 db-connect.php 但每次我加载它时它都显示一个空白日历。我猜我需要一个 Ajax 代码,但我不太了解这些。如果那是我所缺少的,那么我会尝试研究它并添加它。
任何有关问题的帮助都会有所帮助。我已经阅读了很多关于此的“相关”问题,但是 none 这些问题似乎正在纠正我的问题。
EDIT1
db-connect.php returns结果如下,看来是正确的格式
{"id":"1","title":"Test","start":"2021-02-06 10:38:24","end":"2021-02-06 12:38:24"}
服务器端也没有错误
您可以尝试以下方法,首先 returns 将 JSON 响应设置为数组,然后设置正确的响应 header。
<?php
// List of events
$events = array();
// connection to the database
$mysqli = new mysqli("localhost","username","password","database");
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit;
}
//fetches and sends data in string
$result = $mysqli->query("SELECT * FROM events ORDER BY id");
while ($event = $result->fetch_array(MYSQLI_ASSOC)) {
$events[] = $event;
};
// response should be associated with the correct header setting
header('Content-Type: application/json; charset=utf-8');
// now we can JSON the response
echo json_encode($events);
// in case we have some more code afterwards in some if statements
exit;
?>
我一直在摆弄 fullcalendar java 脚本,我似乎无法让日历加载我的 mysql 数据库中的任何数据。
我的 db-connect.php 文件 returns 当我测试它时 table 中的第一个条目,但仍未填充到日历中。
<?php
// List of events
$events = array();
// connection to the database
$mysqli = new mysqli("localhost","username","password","database");
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//fetches and sends data in string
$result = $mysqli->query("SELECT * FROM events ORDER BY id");
$events = $result->fetch_array(MYSQLI_ASSOC);
echo json_encode($events);
?>
我还有一个 get_events.php,我尝试将其设置为仅提取日期范围。当我测试它时它不会提取任何信息,但这应该是因为我在没有日期范围的情况下测试它。
<?php
// List of events
$events = array();
$start = $_GET["start"];
$end = $_GET["end"];
// connection to the database
$mysqli = new mysqli("localhost","agaraapp_events","!QAZ@WSX3edc4rfv","agaraapp_events");
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//fetches and sends data in string
$query = mysqli_query($link, "SELECT * FROM events WHERE start >= '$start' AND end <= '$end'");
while($fetch = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
$e = array();
$e['id'] = $fetch['id'];
$e['title'] = $fetch['title'];
$e['start'] = $fetch['start'];
$e['end'] = $fetch['end'];
array_push($events, $e);
}
echo json_encode($events);
?>
那么这是我的日历代码。
<head>
<meta charset="utf-8" />
<link href="CalendarJava/main.css" rel="stylesheet" />
<script src="CalendarJava/main.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives',
initialView: 'dayGridMonth',
events: {
url: 'get_events.php',
type: 'POST',
data : {
},
success : function(response){
}
}
});
calendar.render();
});
</script>
</head>
我已经将 url 更改为 get_events.php 和 db-connect.php 但每次我加载它时它都显示一个空白日历。我猜我需要一个 Ajax 代码,但我不太了解这些。如果那是我所缺少的,那么我会尝试研究它并添加它。
任何有关问题的帮助都会有所帮助。我已经阅读了很多关于此的“相关”问题,但是 none 这些问题似乎正在纠正我的问题。
EDIT1
db-connect.php returns结果如下,看来是正确的格式
{"id":"1","title":"Test","start":"2021-02-06 10:38:24","end":"2021-02-06 12:38:24"}
服务器端也没有错误
您可以尝试以下方法,首先 returns 将 JSON 响应设置为数组,然后设置正确的响应 header。
<?php
// List of events
$events = array();
// connection to the database
$mysqli = new mysqli("localhost","username","password","database");
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit;
}
//fetches and sends data in string
$result = $mysqli->query("SELECT * FROM events ORDER BY id");
while ($event = $result->fetch_array(MYSQLI_ASSOC)) {
$events[] = $event;
};
// response should be associated with the correct header setting
header('Content-Type: application/json; charset=utf-8');
// now we can JSON the response
echo json_encode($events);
// in case we have some more code afterwards in some if statements
exit;
?>