user9304280
user9304280

Reputation:

Full Calendar - Error Loading JSON events

I am having trouble with Full Calendar (FC) to read the JSON object I have returned from my database to display events on the calendar. Where could I be overlooking?

My jQuery for FC is here:

$('#calendar').fullCalendar({
        defaultView: 'month',
        titleFormat: "MMMM YYYY",
        header: {
            left: 'month,agendaWeek,agendaDay',
            center: 'title',
            right: 'prevYear,prev,next,nextYear'
        },
        events: {
            url: "../ajax/tutor/json_events.php",
            type: "POST",
            color: "yellow",
            textColor: "black",
            error: function() {
                alert('There was an error while fetching events.');
            }
        }
    });

The json_events.php is here:

$returnArray = array();
$userID = filter_input(INPUT_GET, "id");

$connect = db();

$stmt = $connect->prepare("SELECT * FROM Lessons WHERE TutorID = 1");

if($stmt->execute()){
    $results = $stmt->get_result();

    while($row = $results->fetch_assoc()){
        $rowArray['start'] = $row['Start'];
        $rowArray['end'] = $row['End'];
        $rowArray['title'] = $row['CourseTaught'];

        array_push($returnArray, $rowArray);
    }

    $json = json_encode($returnArray);
}

echo print_r($json);

$stmt->close();

The JSON object displays perfectly, however the alert is being thrown from jquery events for some reason, I am just overlooking the error somehow. A fresh pair of eyes would be useful.

Upvotes: 1

Views: 751

Answers (2)

NahimRgz
NahimRgz

Reputation: 26

Add square brackets around $json, something like echo('['.$json.']');

Upvotes: 1

Dan Bowling
Dan Bowling

Reputation: 1235

You are returning a human-readable dump of the variable in PHP by using print_r(). Your JavaScript is expecting the true JSON object without formatting.

Simply echo $json; instead of echo print_r($json); and you will get your json object without the surrounding <pre> tags.

Upvotes: 0

Related Questions