【发布时间】:2018-03-14 20:36:39
【问题描述】:
我正在努力让 FullCalendar 通过网络方法填充事件。
web方法如下:
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Imports Newtonsoft.Json
<ServiceContract(Namespace:="")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class TestService
<OperationContract()>
<WebGet>
Public Function GetEventList(start As Date, [end] As Date) As String
Dim a As String
a = "[ { ""id"": ""46_l"", ""title"": ""CustomEvent-Chargement"", ""allDay"": false, ""start"": ""2018-03-10T14:00:00"", ""end"": ""2018-03-10 15:00""}]"
Return a
End Function
End Class
我在网页上初始化全日历的代码是:
var initializeCalendar = function () {
$('.calendar').fullCalendar({
events: "../../WebServices/TestService.svc/GetEventList",
//events: d=[ { "id": "46_l", "title": "CustomEvent-Chargement", "allDay": false, "start": "2018-03-10T14:00:00", "end": "2018-03-10 15:00"}],
height: screen.height - 160,
});
};
通过使用 Fiddler,我可以看到 web 方法被调用,并返回以下内容:
这是一个有效的 JSON 字符串,但 FullCalendar 不会在日历上填充事件。
如果我换行
events: "../../WebServices/TestService.svc/GetEventList",
与
events: d=[ { "id": "46_l", "title": "CustomEvent-Chargement", "allDay": false, "start": "2018-03-10T14:00:00", "end": "2018-03-10 15:00"}],
FullCalendar 将使用事件填充日历。
我做错了什么?
提前感谢您的帮助。
编辑:
如果我将 fullcalendar 的 events 属性的代码更改为
events: function( start, end, timezone, callback ) {
//manually specified ajax call to the server:
$.ajax({
url: "../../WebServices/TestService.svc/GetEventList",
data: { "start": start.toISOString(), "end": end.toISOString() }, //pass in start and end so the server can return the correct events for the time period being displayed
dataType: "json",
method: "GET",
}).done(function(data) { //success
callback(data.d); //pass the array contained in the "d" property to fullCalendar
}).fail(function(jqXHR) { //failure
alert("An error occurred while fetching events: " + jqXHR.statusText);
});
}
以及返回事件列表的方法
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Imports Newtonsoft.Json
Public Class [Event]
Public Property id() As String
Public Property title() As String
Public Property allDay As Boolean
Public Property start() As String
End Class
<ServiceContract(Namespace:="")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class TestService
<OperationContract()>
<WebGet(ResponseFormat:=WebMessageFormat.Json)>
Public Function GetEventList(start As String, [end] As String) As List(Of [Event])
Dim results As New List(Of [Event])()
results.Add(New [Event]() With {
.id = "46_l",
.title = "CustomEvent-Chargement",
.allDay = False,
.start = "2018-03-10T14:00:00"
})
Return results
End Function
End Class
它返回对象(参见下面的 Fiddler 屏幕截图),但不填充日历。
【问题讨论】:
-
您的服务是按照您所写的方式返回 d=[...],还是返回仅使用内容类型 application/json 标记的数组?
-
进一步阅读上面的内容,当您应该只返回数组时,您将返回
d=[...]。确保 http 标头设置为 application/json。 -
我已经修改了 WebGet 以告诉它以 JSON 格式返回数据
,但我仍然得到完全相同的结果,即 d=[..... ..
标签: vb.net fullcalendar webmethod