FullCalendar: la fuente JSON no funciona, pero el uso de datos JSON directamente sí

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)

¿Alguna idea?

<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)

preguntado el 09 de marzo de 12 a las 16:03

Impossible to help without seeing the code and the JSON (live demo would be even better). -

Asumo "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? -

use browser console to look at the ajax request, will see status and data return (if any) -

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 :) -

2 Respuestas

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.

respondido 12 mar '12, 09:03

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.

Espero que este trabajo para ti.

Respondido el 26 de junio de 15 a las 18:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.