You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FullCalendar在ASP.NET/C#中无法加载事件的问题排查

问题分析与解决方案

嘿,我一眼就看出问题出在哪了——你手动拼接的JSON格式不符合标准,这才导致Firefox的JSON解析报错,FullCalendar没法正常加载事件。

核心问题:非标准JSON格式

标准JSON有两个硬性要求你没做到:

  1. 所有的键(比如id、title这类字段名)必须用双引号包裹,不能省略引号,也不能用单引号
  2. 字符串类型的属性值也必须用双引号包裹,而不是你现在用的单引号

你当前返回的JSON是这种错误格式:

[{id: '1029',title: 'mr 1',clientphone: '1234556654',...}]

而符合规范的标准JSON应该是:

[{"id": "1029","title": "mr 1","clientphone": "1234556654",...}]

这也解释了为什么直接把字符串写进代码里能运行(FullCalendar内部做了宽松解析),但通过AJAX加载时,浏览器的JSON.parse会严格校验格式,直接抛出错误。

最优解决方案:放弃手动拼接,用官方JSON序列化工具

手动拼接JSON简直是给自己挖坑,不仅容易犯格式错误,遇到特殊字符(比如字符串里的引号、换行)时还得手动转义,麻烦又容易出错。

推荐用.NET的专业JSON序列化库来生成标准JSON,比如Newtonsoft.Json(Json.NET)或者.NET Core/.NET 5+自带的System.Text.Json,步骤如下:

  1. 先定义一个和你的事件结构对应的实体类:
public class CalendarEvent
{
    public string id { get; set; }
    public string title { get; set; }
    public string clientphone { get; set; }
    public string clientemail { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public bool allDay { get; set; }
    public string description { get; set; }
}
  1. 把你的事件列表序列化为标准JSON并返回:
// 假设你已经把所有事件整理成了List<CalendarEvent> eventsList
string json = JsonConvert.SerializeObject(eventsList);
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write(json);

这样生成的JSON绝对符合规范,FullCalendar就能正常解析并显示事件了。

如果你非要手动拼接(极度不推荐)

要是你暂时不想改序列化方式,那必须把所有键和字符串值都用双引号包裹,还要注意转义字符串里的特殊双引号,比如:

return "{" + "\"id\": \"" + cevent.id + "\"," + "\"title\": \"" + HttpContext.Current.Server.HtmlEncode(cevent.title) + "\"," + "\"clientphone\": \"" + HttpContext.Current.Server.HtmlEncode(cevent.clientphone) + "\"," + "\"clientemail\": \"" + HttpContext.Current.Server.HtmlEncode(cevent.clientemail) + "\"," + "\"start\": \"" + (cevent.start).ToString("yyyy-MM-dd HH:mm") + "\"," + "\"end\": \"" + (cevent.end).ToString("yyyy-MM-dd HH:mm") + "\"," + "\"allDay\":" + allDay + "," + "\"description\": \"" + HttpContext.Current.Server.HtmlEncode(cevent.description) + "\"" + "},";

但还是那句话,这种方式很容易出问题,强烈建议用序列化工具。

内容的提问来源于stack exchange,提问作者confusedMind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:02:24