Reputation: 50
I'm trying to send a JSON object back to my fullcalendar ajax request and instead it's just returning an array. I'm new to JSON, and PHP, but I've done as much research as I can and haven't found anything useful.
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;
?>
data being returned
[["1","title","2015-07-26 00:00:00","2015-07-26 00:00:00","","1"]]
So, my question is, why is it not returning in a correct JSON key/value pair format?
And I don't think the issue is with the jQuery, but just in case...
ajax call from calendar.js
eventSources: [
{
url: '/php/events.php',
type: 'GET',
data: {},
error: function (data) {
alert('There was an error while fetching events!');
console.log(data);
}
}
],
Upvotes: 0
Views: 640
Reputation: 3322
I think, solution is to loop the result from mysql and generate the required array.
Basically idea is to make sure your .php
generates a valid JSON object as required by full-calendar. Accepted value for eventSources are mentioned in their 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;
?>
I think you are making eventSources
complicated. Try simple one:
eventSources: [
'/php/events.php'
]
Default ajax options will be handled by fullcalendar.
Upvotes: 1