php没有返回JSON对象

Fullcalendar events.php not returning a JSON object

本文关键字:JSON 对象 返回 php      更新时间:2023-09-26

我试图发送一个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的可接受值在它们的文档中有提及

    // 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'
]