duanpo8329 2018-10-12 00:31
浏览 78
已采纳

Fullcalendar不呈现从golang杜松子酒获取的事件

I'm a newbie to golang and web-dev, I'm using gin as an api to provide events for fullcalendar as json. Here are my fullcalendar events configures:

<script>
    $(function(){
        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            defaultView: 'month',
            events: {
                url: '/events',
                type: 'POST'
            }
        })
    });
</script>

Below is my gin handler:

func EventsJSON(c *gin.Context) {
    start := c.PostForm("start")
    end := c.PostForm("end")
    es := models.GetEvents(start, end)
    jes, err := json.Marshal(es)
    if err != nil {
        fmt.Println(err)
    }
    c.JSON(200, gin.H{"events": jes})
}

Below is the []Events returned from db and jes:

[{"id":1,"title":"test event",
  "start":"2018-10-09T20:07:50.402286+08:00",
  "end":"2018-10-09T20:57:50.402286+08:00",
  "description":1},
 {"id":2,"title":"test event",
  "start":"2018-10-11T15:03:43.963198+08:00",
  "end":"2018-10-11T15:53:43.963198+08:00",
  "description":1}]

[91 123 34 105 100 34 58 49 44 34 116 105 116 108 101 34 58 34 116 101 115 116 32 101 118 101 110 116 34 44 34 115 116 97 114 116 34 58 34 50 48 49 56 45 49 48 45 48 57 84 50 48 58 48 55 58 53 48 46 52 48 50 50 56 54 43 48 56 58 48 48 34 44 34 101 110 100 34 58 34 50 48 49 56 45 49 48 45 48 57 84 50 48 58 53 55 58 53 48 46 52 48 50 50 56 54 43 48 56 58 48 48 34 44 34 100 101 115 99 114 105 112 116 105 111 110 34 58 49 125 44 123 34 105 100 34 58 50 44 34 116 105 116 108 101 34 58 34 116 101 115 116 32 101 118 101 110 116 34 44 34 115 116 97 114 116 34 58 34 50 48 49 56 45 49 48 45 49 49 84 49 53 58 48 51 58 52 51 46 57 54 51 49 57 56 43 48 56 58 4848 34 44 34 101 110 100 34 58 34 50 48 49 56 45 49 48 45 49 49 84 49 53 58 53 51 58 52 51 46 57 54 51 49 57 56 43 48 56 58 48 48 34 44 34 100 101 115 99 114 105 112 116 105 111 110 34 58 49 125 93]

But the events are not being rendered on the calendar, I'm not sure if I should convert it with string(jes) before respond to client or it is something wrong with the fullcalendar events config

I checked the respons body and found the events are like this

{"events":"W3siaWQiOjEsInRpdGxlIjoidGVzdCBldmVudCIsInN0YXJ0IjoiMjAxOC0xMC0wOVQyMDowNzo1MC40MDIyODYrMDg6MDAiLCJlbmQiOiIyMDE4LTEwLTA5VDIwOjU3OjUwLjQwMjI4NiswODowMCIsImRlc2NyaXB0aW9uIjoxfSx7ImlkIjoyLCJ0aXRsZSI6InRlc3QgZXZlbnQiLCJzdGFydCI6IjIwMTgtMTAtMTFUMTU6MDM6NDMuOTYzMTk4KzA4OjAwIiwiZW5kIjoiMjAxOC0xMC0xMVQxNTo1Mzo0My45NjMxOTgrMDg6MDAiLCJkZXNjcmlwdGlvbiI6MX1d"}

I do string(jes) and the respond json becomes

{"events":"[{\"id\":1,\"title\":\"test event\",\"start\":\"2018-10-09T20:07:50.402286+08:00\",\"end\":\"2018-10-09T20:57:50.402286+08:00\",\"description\":1},{\"id\":2,\"title\":\"test event\",\"start\":\"2018-10-11T15:03:43.963198+08:00\",\"end\":\"2018-10-11T15:53:43.963198+08:00\",\"description\":1}]"}

if I don't do json.Marshal()

func EventsJSON(c *gin.Context) {
    start := c.PostForm("start")
    end := c.PostForm("end")
    es := models.GetEvents(start, end)
    c.JSON(200, gin.H{"events": es})
}

reponse json becomes like below but still not rendering

{"events":[{"id":1,"title":"test event",
 "start":"2018-10-09T20:07:50.402286+08:00",
 "end":"2018-10-09T20:57:50.402286+08:00","description":1}, 
{"id":2,"title":"test event",
 "start":"2018-10-11T15:03:43.963198+08:00",
 "end":"2018-10-11T15:53:43.963198+08:00","description":1}]}

It turns out c.JSON already serialized it to json, but still, the events not being rendered to the calendar...

展开全部

  • 写回答

1条回答 默认 最新

  • duanfu7004 2018-10-12 04:12
    关注

    It seems not providing the exact response format that fullcalendar can read, so change the handler not to respond with a key solves the problem

    func EventsJSON(c *gin.Context) {
    start := c.PostForm("start")
    end := c.PostForm("end")
    es := models.GetEvents(start, end)
    c.JSON(200, es})
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部