我是golang和Web-dev的新手,我使用gin作为api为fullcalendar提供事件作为json。这是我的全日历事件配置:
<script>
$(function(){
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultView: 'month',
events: {
url: '/events',
type: 'POST'
}
})
});
</script>
下面是我的杜松子酒处理器:
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})
}
下面是[]从db和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]
但是事件未在日历上呈现,我不确定是否应在响应客户端之前使用字符串(jes)对其进行转换,否则fullcalendar事件配置有问题
我检查了响应主体,发现事件是这样的
{"events":"W3siaWQiOjEsInRpdGxlIjoidGVzdCBldmVudCIsInN0YXJ0IjoiMjAxOC0xMC0wOVQyMDowNzo1MC40MDIyODYrMDg6MDAiLCJlbmQiOiIyMDE4LTEwLTA5VDIwOjU3OjUwLjQwMjI4NiswODowMCIsImRlc2NyaXB0aW9uIjoxfSx7ImlkIjoyLCJ0aXRsZSI6InRlc3QgZXZlbnQiLCJzdGFydCI6IjIwMTgtMTAtMTFUMTU6MDM6NDMuOTYzMTk4KzA4OjAwIiwiZW5kIjoiMjAxOC0xMC0xMVQxNTo1Mzo0My45NjMxOTgrMDg6MDAiLCJkZXNjcmlwdGlvbiI6MX1d"}
我做字符串(jes),响应json变成
{"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}]"}
如果我不做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})
}
响应json如下所示,但仍无法呈现
{"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}]}
事实证明c.JSON已经将其序列化为json,但是仍然没有将事件呈现到日历中...
答案 0 :(得分:0)
似乎没有提供fullcalendar可以读取的确切响应格式,因此更改处理程序以使其不使用键即可解决问题
package testpkg;
class Overloading1 {
public void disp(char c) {
System.out.println(c);
}
public void disp(char c, int num) {
System.out.println(c + " " + num);
}
}
class Overloading2 extends Overloading1 {
public static void main(String args[]) {
Overloading2 obj = new Overloading2();
obj.disp('a');
obj.disp('a', 10);
}