FullCalendar JSON不填充日历



我想我缺少一些东西。我已经设置了完整的日历,并且默认版本可正常工作,但是现在正在添加自己的JSON,而不是。

日历页中的代码为

$(document).ready(function() {
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay,listWeek'
        },
        defaultDate: '2017-09-12',
        editable: true,
        navLinks: true, // can click day/week names to navigate views
        eventLimit: true, // allow "more" link when too many events
        events: {
            url: 'php/get-events.php',
            error: function() {
                $('#script-warning').show();
            }
        },
        loading: function(bool) {
            $('#loading').toggle(bool);
        }
    });
});

我正在学习如何进行编码JSON,并且在网上找到了一个教程,这给了我一些似乎有效的代码。我已经修改了get-events.php中的原始代码以进行读取(带有数据库详细信息的摘要)...

// Require our Event class and datetime utilities
require dirname(__FILE__) . '/utils.php';
// Short-circuit if the client did not give us a date range.
if (!isset($_GET['start']) || !isset($_GET['end'])) {
    die("Please provide a date range.");
}
// Parse the start/end parameters.
// These are assumed to be ISO8601 strings with no time nor timezone, like "2013-12-29".
// Since no timezone will be present, they will parsed as UTC.
$range_start = parseDateTime($_GET['start']);
$range_end = parseDateTime($_GET['end']);
// Parse the timezone parameter if it is present.
$timezone = null;
if (isset($_GET['timezone'])) {
    $timezone = new DateTimeZone($_GET['timezone']);
}
class Emp {
    public $id = "";
    public $title  = "";
    public $start = "";
    public $url = "";
}
while(!$JAN->atEnd()) {              
    e = new Emp();
    $e->id = $JAN->getColumnVal("ID");
    $e->title  = $JAN->getColumnVal("TITLE");
    $e->start = $JAN->getColumnVal("DATE")."T".$JAN->getColumnVal("TIME");
    $e->url = "meeting_info.php?ID=".$JAN->getColumnVal("ID");
    echo json_encode($e);  
    $JAN->moveNext();
 }
 $JAN->moveFirst(); //return RS to first record
// Read and parse our events JSON file into an array of event data arrays.
$json = file_get_contents(dirname(__FILE__) . '/../json/events.json');
$input_arrays = json_decode($json, true);
// Accumulate an output array of event data arrays.
$output_arrays = array();
foreach ($input_arrays as $array) {
// Convert the input array into a useful Event object
$event = new Event($array, $timezone);
// If the event is in-bounds, add it to the output
if ($event->isWithinDayRange($range_start, $range_end)) {
    $output_arrays[] = $event->toArray();
}
}
// Send JSON to the client.
echo json_encode($output_arrays);

当我独自运行get-events.php页面时,我会得到我假设的正确编码的JSON,数组中的一个示例是...

{"id":20,"title":"Executive Committee Meeting","start":"2017-05-01T00:00:00","url":"meeting_info.php?ID=20"}

有人可以告诉我我做错了什么吗?

您需要在完整的PHP对象上运行json_encode(),而不是在每个PHP对象上单独运行。在循环中,将每个EMP添加到数组中,然后在循环结束时编码数组。

如果您在Ajax请求的结果中查看浏览器的网络选项卡,我认为您很可能会看到一串单个对象,但不会包裹在数组(Square)括号中,而不是由逗号隔开,这意味着JSON无效。您的浏览器控制台中有关无效数据格式的错误消息也很有可能。最好检查一下,而不是假设您的JSON是正确的。您还可以将其粘贴到在线JSON验证器工具,以孤立地验证JSON。

这样的事情应该更好地工作:

$events = array();
while(!$JAN->atEnd()) {              
    e = new Emp();
    $e->id = $JAN->getColumnVal("ID");
    $e->title  = $JAN->getColumnVal("TITLE");
    $e->start = $JAN->getColumnVal("DATE")."T".$JAN->getColumnVal("TIME");
    $e->url = "meeting_info.php?ID=".$JAN->getColumnVal("ID");
    $events[] = $e; //add event to the array
    $JAN->moveNext();
 }
 echo json_encode($events); //encode the whole array as a coherent piece of JSON
//P.S. no need to run moveFirst really, since the request is going to end, and discard the resultset anyhow. Depending on your data access technique, you possibly need to close the recordset though, to avoid locking etc.

您需要的代码来生成(以及FullCalendar所期望的),是一个JSON数组 - 这是一个简单的示例,包含2个元素(表示事件):

[
  { "id":20, "title":"Executive Committee Meeting", "start":"2017-05-01T00:00:00", "url":"meeting_info.php?ID=20" },
  { "id":21, "title":"Another Boring Committee Meeting", "start":"2017-05-02T00:00:00", "url":"meeting_info.php?ID=21" }
]

我上面给出的示例代码应生成一个与此JSON样本相同格式的数组。

相关内容

  • 没有找到相关文章

最新更新