I am trying to integrate events into the fullcalendar plugin. A web method in my ASPX file generates JSON data for the JavaScript code. However, I am facing difficulty in connecting the result of the web method with the full calendar. Currently, I can only manually add events.
Below is the JavaScript code:
$(document).ready(function () {
$('#btnInit').click(function () {
var start = Date.parse($("#MainContent_dateD").text());
var end = Date.parse($("#MainContent_dateF").text());
var cle = $("#MainContent_HF_cleU").val();
$.ajax({
type: "POST",
url: "ConsultationPlanning.aspx/getPlanning",
data: '{"start": "' + start + '", "end": "' + end + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg == null) {
alert('no result');
return;
}
alert("received: " + msg.d);
document.getElementById("MainContent_dateD").innerHTML = msg.d;
$('#calendar').fullCalendar({
eventSources: JSON.parse(msg.d)
});
},
error: function(msg){
alert("error: " + msg);
}
});
});
// FullCalendar initialization
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
hiddenDays: [0],
defaultView: 'month',
editable: false,
allDaySlot: false,
selectable: false,
eventSources: [{
url: 'ConsultationPlanning.aspx/getPlanning'
}]
});
})
In the initial setup, the parameters in the web methods were strings. Here is the code from the ASPX.CS file:
public static String getPlanning(string start, string end)
{
List<String> ls1 = new List<string>();
IList<Planning> ls2= new List<Planning>();
DateTime t = Convert.ToDateTime(start);
DateTime t2 = t.AddHours(1.0);
Planning p=new Planning();
for (int i = 0; i < 4; i++)
{
p.id = "" + i + "" ;
p.title = "event "+i ;
p.start = String.Format("{0:s}", t.AddDays(Convert.ToDouble(i)));
p.end = String.Format("{0:s}", t2.AddDays(Convert.ToDouble(i)));
ls2.Add(p);
}
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(ls2);
return sJSON;
}
I have validated the JSON data using jsonlint.com, so I believe the issue lies within the JavaScript code, but I cannot locate it.
Here is an example of the JSON data:
[
{"id":"0","title":"event 0","start":"2015-05-04T12:35:37","end":"2015-05-04T13:35:37"},
{"id":"1","title":"event 1","start":"2015-05-05T12:35:37","end":"2015-05-05T13:35:37"},
{"id":"2","title":"event 2","start":"2015-05-06T12:35:37","end":"2015-05-06T13:35:37"},
{"id":"3","title":"event 3","start":"2015-05-07T12:35:37","end":"2015-05-07T13:35:37"}
]