我有一个Web方法将JSON作为事件数据返回到fullCalendar,但是添加了“()”导致解析器错误。
我能够剪掉不想要的“()”,然后用jQuery附加事件,但显然不想这样做。
数据来源是使用Razor的网络方法。使用JSON帮助程序对数据进行编码会产生格式良好的JSON字符串,即没有“()”。当然,如果我使用encode I,那么将使用Response.Write将数据发送回AJAX函数。如果我使用JSON.Write(data,Response.Output),则会收到相同的错误形成的JSON数据。
在成功函数中捕获返回的数据会显示附带“()”的数据。
以下是返回数据的Web方法的一部分:
// convert the header names and data to strings
var rows = from e in cE
select new
{
id = e.EvId,
title = e.Title,
start = e.startT,
allDay = false,
end = e.endT,
backgroundColor = e.eventColor
};
string mJson = Json.Encode(rows);
//Json.Write(rows, Response.Output);
Response.Write(mJson.Trim());
这是编码的结果:
"[{\"id\":9,\"title\":\"new event\",\"start\":\"2012-05-29 19:00:00\",\"allDay\":false,\"end\":\"2012-05-29 20:00:00\",\"backgroundColor\":\"Orange \"},{\"id\":9,\"title\":\"new event\",\"start\":\"2012-06-05 19:00:00\",\"allDay\":false,\"end\":\"2012-06-05 20:00:00\",\"backgroundColor\":\"Orange \"},{\"id\":9,\"title\":\"new event\",\"start\":\"2012-06-12 19:00:00\",\"allDay\":false,\"end\":\"2012-06-12 20:00:00\",\"backgroundColor\":\"Orange \"},{\"id\":10,\"title\":\"another\",\"start\":\"2012-06-22 19:00:00\",\"allDay\":false,\"end\":\"2012-06-22 19:45:00\",\"backgroundColor\":\"Orange \"},{\"id\":10,\"title\":\"another\",\"start\":\"2012-06-29 19:00:00\",\"allDay\":false,\"end\":\"2012-06-29 19:45:00\",\"backgroundColor\":\"Orange \"}]" string
以下是AJAX成功函数显示为收到的数据:
"[{"id":9,"title":"new event","start":"2012-05-29 19:00:00","allDay":false,"end":"2012-05-29 20:00:00","backgroundColor":"Orange "},{"id":9,"title":"new event","start":"2012-06-05 19:00:00","allDay":false,"end":"2012-06-05 20:00:00","backgroundColor":"Orange "},{"id":9,"title":"new event","start":"2012-06-12 19:00:00","allDay":false,"end":"2012-06-12 20:00:00","backgroundColor":"Orange "},{"id":10,"title":"another","start":"2012-06-22 19:00:00","allDay":false,"end":"2012-06-22 19:45:00","backgroundColor":"Orange "},{"id":10,"title":"another","start":"2012-06-29 19:00:00","allDay":false,"end":"2012-06-29 19:45:00","backgroundColor":"Orange "}]();???"