javascript - Fullcalendar events.php 不返回 JSON 对象

标签 javascript php jquery ajax json

我试图将 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/

相关文章:

javascript - 更改 CKEditor 实例的配置选项

php - MySQL 数据的不同表格单元格颜色

带有 ping 和 mysql 查询结果的 php 主机状态

php - 退出 Smarty 手动完成

javascript - 在 ASP.NET Web 应用程序中包含 Google Analytics javascript 代码

javascript - 如何从 PHP 调用 Bootstrap 模式弹出窗口

javascript - 了解类 : Compose a Triangle from extending 3 points?

javascript - 如何在我的js数据文件更改时刷新页面

javascript - append(string/ref_jquery_obj/jquery_obj) 中的 jQuery 差异

javascript - 防止非固定元素在到达固定元素的末尾时滚动