【问题标题】:Populate FullCalendar with events from web method使用来自 Web 方法的事件填充 FullCalendar
【发布时间】: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


【解决方案1】:

Fullcalendar 要求事件数组位于 JSON 的顶层 - 即整个 JSON 是一个数组。而您的 webmethod 返回的是具有单个属性“d”的对象。因此 fullCalendar 无法识别事件,因为它不知道在该属性中查找事件数组。

这似乎是 webmethods 的正常行为,我不确定是否真的有任何解决方法,所以最简单的方法是使用 fullCalendar 的自定义“事件”功能。然后,您可以告诉它专门查看 JSON 的“d”属性以查找数据。

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

更多详情请见https://fullcalendar.io/docs/events-function

如果 ajax 调用没有正确连接,您可能需要调整它的一些设置,但它应该是正确的或几乎正确的。


附:我假设您的 webmethod 中的上述数据仅用于测试,但我强烈建议不要像那样手动构建您的 JSON - 在处理真实事件数据。

【讨论】:

  • 感谢您非常全面的回复。我完全按照你指定的方式实现了代码,它返回了一个对象(我可以通过 Fiddler 看到),但它仍然没有填充日历。
  • 嗯,请告诉我你可以在 Fiddler 中看到的确切响应。
  • 更新帖子以反映使用 JQuery AJAX 的调用
  • 您能否显示实际的 JSON,而不是图形表示。如果您的浏览器无法显示原始响应,则要么使用具有更好开发工具的浏览器(例如 Chrome),要么将 console.log(JSON.stringify(data)); 作为“完成”回调的第一行,然后从控制台粘贴。跨度>
  • 我决定创建一个新的解决方案来测试它,并且只复制代码的必要部分。这样做,我发现我正在调用一个 javascript 函数,该函数正在填充日历并阻止事件函数工作。现在它起作用了!非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多