我试图将 JSON 对象发送回我的 fullcalendar ajax 请求,但它只是返回一个数组。我是 JSON 和 PHP 的新手,但我已经做了尽可能多的研究,但没有发现任何有用的东西。
events.php
<?php
// List of events
$json = array();
// Query that retrieves events
$requete = "SELECT * FROM `evenement` ORDER BY `id`";
// connection to the database
require_once 'mysqli_connect.php';
// Execute the query
$resultat = $dbc->query($requete) or die(print_r($dbc->errorInfo()));
// sending the encoded result to success page
$tempjson = json_encode($resultat->fetch_all(PDO::FETCH_ASSOC));
$tempjson = str_replace('"false"', 'false', $tempjson);
echo $tempjson;
?>
正在返回数据
[["1","title","2015-07-26 00:00:00","2015-07-26 00:00:00","","1"]]
所以,我的问题是,为什么它没有以正确的 JSON 键/值对格式返回?
我认为问题不在于 jQuery,但以防万一......
来自calendar.js的ajax调用
eventSources: [
{
url: '/php/events.php',
type: 'GET',
data: {},
error: function (data) {
alert('There was an error while fetching events!');
console.log(data);
}
}
],
最佳答案
我认为,解决方案是循环mysql的结果并生成所需的数组。
基本想法是确保您的 .php
生成完整日历所需的有效 JSON 对象。 eventSources 的可接受值在其 doc 中提到。
// Query that retrieves events
$requete = "SELECT * FROM `evenement` ORDER BY `id`";
// connection to the database
require_once 'mysqli_connect.php';
// Execute the query
$resultat = $dbc->query($requete) or die(print_r($dbc->errorInfo()));
// sending the encoded result to success page
while($row = $resultat->fetch_assoc()){
$array[] = $row;
}
$tempjson = json_encode($array);
$tempjson = str_replace('"false"', 'false', $tempjson);
echo $tempjson;
?>
我认为您使 eventSources
变得复杂。尝试简单的一个:
eventSources: [
'/php/events.php'
]
默认 ajax 选项将由 fullcalendar 处理。
关于javascript - Fullcalendar events.php 不返回 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31633728/