javascript - Fullcalendar 没有加载我的 json 来显示事件
问题描述
我一直在搞乱fullcalendar java脚本,我似乎无法让日历加载我的mysql数据库中的任何数据。
我的 db-connect.php 文件在我测试时返回表中的第一个条目,但仍未填充到日历中。
<?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 代码,但我对这些不太了解。如果那是我所缺少的,那么我将尝试对其进行研究并将其添加进去。
任何有关出现问题的帮助都会有所帮助。我已经阅读了一堆关于此的“相关”问题,但这些问题似乎都没有解决我的问题。
编辑1
db-connect.php 返回以下结果,这似乎是正确的格式
{"id":"1","title":"Test","start":"2021-02-06 10:38:24","end":"2021-02-06 12:38:24"}
服务器端也没有错误
解决方案
您可以尝试以下操作,首先将 JSON 响应作为数组返回,然后设置正确的响应标头。
<?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;
?>
推荐阅读
- javascript - 关闭谷歌广告管理器标签中的 div 脚本
- mongodb - mongodb中是否有类似于日志的读取操作?
- android - 尝试将文件上传到 OData 服务时出现 DataStreamException
- field - FileMaker Pro TextColor 作为表中字段的计算
- reactjs - React Material ui - 居中项目表单
- javascript - 获取会话过期通知 [Node.js / Express 会话]
- teradata - 如何在 teradata 中验证 XML 格式是否正确
- java - ModelMapper - 扁平化对象的简单方法
- python - 无法使用 Cluster_Ensembles 库
- android - 在 Android (flutter) 应用程序中安全地保存 API 密钥