完整日历没有加载我的 json 来显示事件



我一直在搞乱与完整的日历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: %sn", 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: %sn", 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代码,但我不是很熟悉这些。如果这是我所缺少的,那么我将尝试研究并添加它。

任何对问题的帮助都是有帮助的。我读了很多"相关"的这个问题,但这些问题似乎都没有解决我的问题。

EDIT1

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: %sn", 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;
?>

相关内容

  • 没有找到相关文章

最新更新