1

I am trying to use JQuery Full Calendar along with Spring MVC + Freemarker.

I have made a demo like that.

Target: I need to send the calendar's data I just put to the controller to handle it.

Issue: When I send the data to the contoller ,The String parameter I recieve on the contoller has "undefined=undefined" !!

Freemarker:

[#ftl /]
<script type="text/javascript">
    var calendar;
    var calendarData;
    function sendDataViaAjax() {
         $.ajax(
            {
              url:"[@spring.url '/vacation/setVacation'/]",
              type: "POST",
              data: calendarData,
              dataType: "json",
              contentType: "application/json"
            } );
    }


    $(document).ready(function() {
        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
        $.getJSON('[@spring.url '/vacation/getVacation'/]', function (data) {
            calendar = $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                selectable: true,
                selectHelper: true,
                select: function(start, end, allDay) {
                    var title = prompt('Event Title:');
                    if (title) {
                        calendar.fullCalendar('renderEvent',
                        {
                            title: title,
                            start: start,
                            end: end,
                            allDay: allDay
                        },
                                true // make the event "stick"
                                );
                    }
                    calendar.fullCalendar('unselect');
                },
                editable: true,
                events:[data]
            });
          calendarData=data;
        });
    });


</script>
<style type='text/css'>


    body {
        margin-top: 40px;
        text-align: center;
        font-size: 14px;
        font-family: "Lucida Grande", Helvetica, Arial, Verdana, sans-serif;
    }

    #calendar {
        width: 900px;
        margin: 0 auto;
    }

</style>
<body>
<div id='calendar'></div>

<button value="Test" onclick="sendDataViaAjax()"
</body>

Java Controller whose String parameter comes as "undefined=undefined" :

 @RequestMapping(value = "/vacation/setVacation", method = RequestMethod.POST)
      public
      @ResponseBody
      String setVacation( @RequestBody String  response) {

       //Rest of code
      }

I have changed a little bit my freemarker like the following:

<script type="text/javascript">
    var calendar;
    var calendarData;

    function doAjax() {
        var test = JSON.stringify(calendarData);

        $.ajax(
        {
            url:"[@spring.url '/vacation/setVacation'/]",
            type: "POST",
            data :test ,
            dataType: "json",
            contentType: "application/json"
        });
    }


    $(document).ready(function() {
        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
        $.getJSON('[@spring.url '/vacation/getVacation'/]', function (data) {
            calendar = $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                selectable: true,
                selectHelper: true,
                select: function(start, end, allDay) {
                    var title = prompt('Event Title:');
                    if (title) {
                        calendar.fullCalendar('renderEvent',
                        {
                            title: title,
                            start: start,
                            end: end,
                            allDay: allDay
                        },
                                true // make the event "stick"
                                );
                    }
                    calendar.fullCalendar('unselect');
                },
                editable: true,
                events:[data]   //To allow Calendar to render data ,I use events:data instead
            });
            calendarData = data;
        });
    });


</script>

By this way I can post json String to the controller and from there I can continue what I need. What it's killing me that now the calendar is rendered empty without the events I passed while initializing. However, In order to render Calendar data I make a small change ,as mentioned on the comment above. By this way, whenever I try to post json String,I get the following error :"Uncaught TypeError: Converting circular structure to JSON" . I have made many trials but I still have the mentioned issues !! Anyone has an idea about what's going on there !!

Gordon
  • 312,688
  • 75
  • 539
  • 559
Echo
  • 2,959
  • 15
  • 52
  • 65

1 Answers1

0

Some of the things which i can think of which help you in resolving are

  1. please check with an alert or firebug that, what is the value for the variable "calendar". Is that the value that is expected by you.
  2. instead of the ajax POST you can try the other option of the REST patter "PUT" and change the same in the controller as well.
  3. You can try using RequestParam annotation along with the RequestBody.
raddykrish
  • 1,866
  • 13
  • 15
  • Thanks for reply.I've tried them. 1-Values of "calendar" is undefined. 2-I've got same result when I use PUT. The issue here that I need to send that calendar object to the controller .The issue is why "calendar" variable is undefined although it's a global variable !! – Echo Apr 15 '12 at 13:24
  • I saw the html of your demo page. Could you please try moving the script part after the html code. I am thinking that the script gets executed before the page gets rendered. Its just a try. – raddykrish Apr 15 '12 at 13:44
  • The code that you pasted here is not the same as in the demo page. I believe that the "#calendar" is a div. I believe the posting elements should be under a form in html and also div is not a valid element under html to be posted for a form. Could you try setting the value of the calendar variable to a hidden input and pass this input's value to your post call. – raddykrish Apr 15 '12 at 13:51
  • I've made a small modification over my freemarker.Please see updates at my code. Now the Object I need to pass which is "calendarData" has data but the issue that when I try to send to the controller , I get that over my console "Uncaught RangeError: Maximum call stack size exceeded" All what I need is to pass the json string !! – Echo Apr 15 '12 at 14:06
  • Any ideas how to send just the json String instead of the whole object that casues to thow an exception !! – Echo Apr 15 '12 at 14:10