0

I am trying to load events, that I have in my database, to my calendar but my little knowledge makes me not find the error

`My Menu Servlet code`

ArrayList<DTO> list = ges.FechasAlquileresAdeudados();
        Gson jsonList = new Gson();
        String json = jsonList.toJson(list);
        PrintWriter out = response.getWriter();
        out.println(json);

/// JS code ///

$('#calendar').fullCalendar({
        header: { ... },
        drop: function (date, allDay) { ... },
        events: BuscarVtos()
      });
    }();

   function BuscarVtos() {
    $.ajax({
        url: 'Menu',
        type: 'POST',
        contentType: 'application/x-www-form-urlencoded; charset=utf-8',
        datatype: 'json',
        success: function (resultado) {
            if (resultado !== "[]\r\n") {
                var json = eval('(' + resultado + ')');
            }
        }
       });
   }
   ;

the ajax response is
   [
    {
        "title": "Title 1",
        "start": "2022-11-05T00:00:00",
        "url": "http://localhost:26231/Sistem1"
    },
    {
        "title": "Title 1",
        "start": "2022-11-10T00:00:00",
        "url": "http://localhost:26231/Sistem2"
    },
    {
        "title": "Title 1",
        "start": "2022-11-09T00:00:00",
        "url": "http://localhost:26231/Sistem3"
    }
   ]

but it does not recognize the data and therefore does not enter the events to the calendar Any Helpme

  • You're not returning anything from `BuscarVtos()`, but you can't easily do that anyway because the AJAX call is async, so the BuscarVtos() function will end before the AJAX request completes. But fullCalendar already has a way to get round this. Read https://fullcalendar.io/docs/v3/events-function for details. – ADyson Nov 29 '22 at 11:16
  • As an aside, why are you using such an old version of fullCalendar? v5.11 is the latest? But you seem to be using v3. Unless you're just updating an old site which was already using it, there's really no reason to start with a very old version like that. – ADyson Nov 29 '22 at 11:19
  • Also some asides regarding the AJAX code. `contentType: 'application/x-www-form-urlencoded; charset=utf-8',`...this isn't necessary unless you're actually sending some content, which you don't seem to be. And it's the default anyway, even if you're not. And `var json = eval('(' + resultado + ')');`...this is dangerous and also unnecessary. I assume you're not familiar with `JSON.parse()`? This is the correct way to parse JSON strings using JavaScript. eval() should be avoided unless you have absolutely no other alternative. – ADyson Nov 29 '22 at 11:21
  • But... `datatype: 'json'` is a typo: it should be `dataType: 'json'` (check the jquery $.ajax documentation, and remember that JS is case-sensitive). If you implement that correctly then (again as per the jQuery documentaiton), jQuery will run JSON.parse on the response for you automatically and then your `resultado` variable will already be a parsed JS object/array ready for you to use directy - meaning there's no need for extra calls to JSON.parse or eval anyway. https://api.jquery.com/jquery.ajax/ – ADyson Nov 29 '22 at 11:23
  • Also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse – ADyson Nov 29 '22 at 11:24

0 Answers0