0
votes

I have FullCalendar setup up correctly on my page and I am trying to get it to read a json source.

My json source works fine when I access it directly via the url, and if I copy the data outputted on screen and put this directly into the FullCalendar events, the event shows up.

Setting the events to the url of my json source just doesnt work. I have used Firebugs NET panel to inspect the call, and it is correctly locating the JSON source, and is fetching the correct data, but simply not showing it on the calendar.

I have set id, title, allday (to true) start (both unix and yyyy-mm-dd format) and end (despite this being optional)

Any ideas?

<script>
$(document).ready(function() {
    $('#calendar').fullCalendar({
        events: "eventsjson" // links to a controller in my symfony2 project. accessing cirectly gives correct json output
    });
});
</script>

Data given by json source:

{"id":1,"title":"Test Event","start":1331309234,"allDay":true}

Inserting that ^^^ straight into the events setting work no problem. (the timestamp was generated by php's time() function)

2
Impossible to help without seeing the code and the JSON (live demo would be even better).James McLaughlin
I assume "eventsjson" is pseudo code, which is actually a valid URL on to a page on the same domain (http://sameDomain.com/feed.html) in your real system?JAAulde
use browser console to look at the ajax request, will see status and data return (if any)charlietfl
JAAulde - I'm afraid you assume wrong. 'eventsjson' is a valid url. It points to a controller in the Symfony2 framework that returns a basic JSON object, and is 100% valid :)Mr Pablo

2 Answers

2
votes

The issue is with PHP's natural behaviour to NOT include the [] brackets when there is only 1 entry in the JSON object.

If a second is added, the [] brackets are added by PHP and the FullCalendar plugin works correctly.

I believe this to be the fault of FullCalendar in all honesty, and have filed it as a bug with the author.

1
votes

I had working in this problem for many hours until I found the solution.

In my project, I'm working with CakePHP v3 and FullCalendar v2, and both are well configured. If I set events in array mode, it worked well... but didn't in URL mode. The same problem of many: FullCalendar get the script (my action in my controller) and take de JSON data but anyone event is showed in the calendar.

The JSON response I build is valid (I validated at http://jsonlint.com/), but anyone events isn't showing at FullCalendar.

This was my response that didn't work:

{
    "events": [
        {
            "id": "1",
            "title": "event1",
            "start": 1435336659,
            "end": 1436200659,
            "allday": false
        },
        {
            "id": "2",
            "title": "event2",
            "start": "2015-07-06 11:37:39",
            "allday": false
        }
    ]
}

And I build this response with this code (in my controller):

$this->set('events', $event_array);
$this->set('_serialize', ['events']);

The problem was the way I set the variables to the JSON view. The solution is:

$this->set(['events' => $event_array, '_serialize' => 'events']);

You can check this at the Cake\View\JsonView class. This code build the following JSON response:

[
    {
        "id": "1",
        "title": "event1",
        "start": 1435336961,
        "end": 1436200961,
        "allday": false
    },
    {
        "id": "2",
        "title": "event2",
        "start": "2015-07-06 11:42:41",
        "allday": false
    }
]

That's all... the problem is the outer "{}" and the "events" keyword that isn't valid for FullCalendar.

I hope this work for you.