C# 即使JSON提要正确,也不显示fullCalendar事件
和其他一些人一样,我在日历中呈现JSON提要事件时遇到了问题。问题通常是JSON格式错误,但事实并非如此,因为我已经用JSONlint对其进行了验证,并在Site.Master中对JSON提要进行了硬编码,结果是肯定的 FireBug正确地获得了JSON响应,但它仍然没有显示在fullCalendar中。我没有主意了 FireBug响应: [{id:1,title:TESTTITLE,info:infoinfo,开始:2012-08-20T12:00:00,结束:2012-08-20T12:00:00,用户:1}] JSON.aspxC# 即使JSON提要正确,也不显示fullCalendar事件,c#,asp.net,json,jquery,fullcalendar,C#,Asp.net,Json,Jquery,Fullcalendar,和其他一些人一样,我在日历中呈现JSON提要事件时遇到了问题。问题通常是JSON格式错误,但事实并非如此,因为我已经用JSONlint对其进行了验证,并在Site.Master中对JSON提要进行了硬编码,结果是肯定的 FireBug正确地获得了JSON响应,但它仍然没有显示在fullCalendar中。我没有主意了 FireBug响应: [{id:1,title:TESTTITLE,info:infoinfo,开始:2012-08-20T12:00:00,结束:2012-08-20T12:00
public partial class JSON : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Get events from db and add to list.
DataClassesDataContext db = new DataClassesDataContext();
List<calevent> eventList = db.calevents.ToList();
// Select events and return datetime as sortable XML Schema style.
var events = from ev in eventList
select new
{
id = ev.event_id,
title = ev.title,
info = ev.description,
start = ev.event_start.ToString("s"),
end = ev.event_end.ToString("s"),
user = ev.user_id
};
// Serialize to JSON string.
JavaScriptSerializer jss = new JavaScriptSerializer();
String json = jss.Serialize(events);
Response.Write(json);
Response.End();
}
}
Site.master
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<link href='fullcalendar/fullcalendar.css' rel='stylesheet' type='text/css' />
<script src='jquery/jquery-1.7.1.min.js' type='text/javascript'></script>
<script src='fullcalendar/fullcalendar.js' type='text/javascript' ></script>
<script type="text/javascript">
$(document).ready(function () {
$('#fullcal').fullCalendar({
eventClick: function() {
alert('a day has been clicked!');
},
events: 'JSON.aspx'
})
});
</script>
我已经扫描相关问题好几天了,但似乎没有一个能解决我的问题…试试这个,你必须在aspx文件中有一个webmethod,fullcalendar可以异步调用它
$(document).ready(function () {
$('#fullcal').fullCalendar({
eventClick: function() {
alert('a day has been clicked!');
},
events: function (start, end, callback) {
$.ajax({
type: "POST", //WebMethods will not allow GET
url: "json.aspx/GetEvents", //url of a webmethod - example below
data: "{'userID':'" + $('#<%= hidUserID.ClientID %>').val() + "'}", //this is what I use to pass who's calendar it is
//completely take out 'data:' line if you don't want to pass to webmethod - Important to also change webmethod to not accept any parameters
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (doc) {
var events = []; //javascript event object created here
var obj = $.parseJSON(doc.d); //.net returns json wrapped in "d"
$(obj.event).each(function () { //yours is obj.calevent
events.push({
title: $(this).attr('title'), //your calevent object has identical parameters 'title', 'start', ect, so this will work
start: $(this).attr('start'), // will be parsed into DateTime object
end: $(this).attr('end'),
id: $(this).attr('id')
});
});
callback(events);
}
});
}
})
那么这将出现在json.aspx中
[WebMethod(EnableSession = true)]
public static string GetEvents(string userID)
{
DataClassesDataContext db = new DataClassesDataContext();
List<calevent> eventList = db.calevents.ToList();
// Select events and return datetime as sortable XML Schema style.
var events = from ev in eventList
select new
{
id = ev.event_id,
title = ev.title,
info = ev.description,
start = ev.event_start.ToString("s"),
end = ev.event_end.ToString("s"),
user = ev.user_id
};
// Serialize to JSON string.
JavaScriptSerializer jss = new JavaScriptSerializer();
String json = jss.Serialize(events);
return json;
}
可能是重复的是,这是一个更新。我不知道如何删除它。我会在这里发布一个答案,所以不要删除这个好的,我在发布后编辑了一段时间,并在里面放了很多评论,如果你在我发布后立即尝试,现在就不同了,试试这个是的,我在发布后直接尝试了,但没有效果。我会再试一次!谢谢,我尝试了新代码,省去了数据字段,并更改了GetEvents的参数,现在fullcalendar消失了。如果fullcalendar消失了,那么其中有一些无效的javascript,导致它无法正常工作。谢谢Scott。我更新到了最新的完整日历,现在可以使用了!