JQuery Full Calendar with Spring MVC ..无法调用 Controller 来获取JSON对象

标签 jquery json spring-mvc fullcalendar

我正在尝试使用 JQuery Full Calendar以及 Spring MVC + Freemarker。

我做了一个类似 that 的演示.

目标:我需要调用 Controller 来获取包含要在日历上呈现的事件的 JSON 对象。

问题: 我有以下 freemarker,它应该转到 Controller 并获取要渲染的 JSON 对象,但它没有去?!!

自由标记:

[#ftl/]

<script type="text/javascript">
    $(document).ready(function() {

        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();

        var 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: [
                {
                    url: '[@spring.url '/vacation/getVacation'/]',
                    type: 'GET',

                    data: {
                        start: 'start',
                        id: 'id',
                        title: 'title'
                    }

                }
            ]
        });

    });

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;
}

Controller :

@RequestMapping(value = "/vacation/getVacation", method = RequestMethod.GET)
    public @ResponseBody   void getVacation(HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 111);
        map.put("title", "event1");
        map.put("start", "2011-07-28");
        map.put("url", "http://yahoo.com/");

        // Convert to JSON string.
        String json = new Gson().toJson(map);


        // Write JSON string.
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().write(json);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Firebug 射击: enter image description here

最佳答案

终于,我成功了:) 我使用 $.getJSON 来获取 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) {
            var 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]
            });
         });
        });

Java Controller :

@RequestMapping(value = "/vacation/getVacation", method = RequestMethod.GET)
    public
    @ResponseBody
    String getVacation(HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 111);
        map.put("title", "event1");
        map.put("start", "2012-4-15");
        map.put("url", "http://yahoo.com/");

        // Convert to JSON string.
        String json = new Gson().toJson(map);

        // Write JSON string.
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        return json;
    }

关于JQuery Full Calendar with Spring MVC ..无法调用 Controller 来获取JSON对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10154113/

相关文章:

jquery - 使用 jQuery 创建后 <select> 中没有值

jquery - 获取此 URL 的最后一部分

Java Spring - 检查用户是否允许查看页面

javascript - 获取选定列的索引(DataTables + ColVis)

有效 JSON 的 Python json.load(file) 错误

c# - .Net 4.0 上的 ServiceStack.Text JSON 解析

javascript - 如何在单击输入时从对象中过滤数据?

java - 返回并转换 ResponseEntity<List<T>>

java - 将 RestTemplate 与 SSL 一起使用

javascript - 如何访问 JavaScript 文件中的 Angular 范围变量