I have a controller that takes the info I need from the DB and I structure it into an array, and then into a JSON with json_encode.
But then, the error I get in the browser is this: Error from the Chrome console
This image is the JSON from my controller My JSON from the controller
Controller
<?php require_once '../classes/functions.php'; $newEvent = new calendario(); $events = $newEvent->getEvents(); $newEvent = null; foreach ($events as $event) { $data[] = array( 'title' => $event['titulo'], 'start' => $event['inicio'], 'end' => $event['fin'], ); header('Content-Type: application/json'); } print_r(json_encode($data));
Full calendar code on my page
var calendarEl = document.getElementById('calendar'); if (calendarEl) { document.addEventListener('DOMContentLoaded', function() { var calendar = new FullCalendar.Calendar(calendarEl, { locale: 'ca', plugins: ['dayGrid', 'timeGrid', 'list', 'bootstrap'], timeZone: 'UTC', themeSystem: 'bootstrap', header: { left: 'today, prev, next', center: 'title', right: 'dayGridMonth,timeGridWeek,timeGridDay,listMonth' }, buttonIcons: { prev: 'fe-arrow-left', next: 'fe-arrow-right', prevYear: 'left-double-arrow', nextYear: 'right-double-arrow' }, weekNumbers: true, events: { url: '../controllers/getEvents.php', type: 'GET', format: 'json', failure: function() { alert('Hem trobat errors quan hem intentat exportar les visites de la base de dades!'); }, }, // events: 'https://fullcalendar.io/demo-events.json', // events: '../controllers/getEvents.php', }); calendar.render(); }); }
For the sake of trying, you will see that I tried to put the demo fullcalendar uses in their examples, this works flawlessly, but my JSON does not…
Answer
So, the problem was with functions.php. As @ADyson commented, there was a meta tag there and with the require_once I was printing it every time.
Thanks to everyone that commented and helped me, and thanks again @ADyson.