Jquery 如何将ajax请求连接到事件?
我已经创建了下面的两个函数,它们从我的API中获得了我需要的东西。但我不知道如何将其放入fullCalendar实例的“事件”部分 这是我的ajax调用:Jquery 如何将ajax请求连接到事件?,jquery,ajax,fullcalendar,Jquery,Ajax,Fullcalendar,我已经创建了下面的两个函数,它们从我的API中获得了我需要的东西。但我不知道如何将其放入fullCalendar实例的“事件”部分 这是我的ajax调用: $.fn.CalendarEvents = (function () { return $.ajax("/api/exams/", { type: "GET", data: JSON.stringify(this), contentType: "application/json",
$.fn.CalendarEvents = (function () {
return $.ajax("/api/exams/", {
type: "GET",
data: JSON.stringify(this),
contentType: "application/json",
success: function(data, status, XHR) {
callEvents(data);
}
});
});
这是我的回调函数:
function callEvents(response) {
var calObj = [];
$.each(response, function (index, item) {
var evt = {
title: item.title,
start: item.startDateTime,
end: item.endDateTime
};
calObj.push(evt);
});
return calObj;
//this writes out exactly what I need to go into my calendar
console.log(calObj);
};
var calendar = $('#calendar').fullCalendar({
events: getCalendarEvents
...
var getCalendarEvents = function(start, end, timezone, callback) {
$.ajax("/api/exams/", {
type: "GET",
data: JSON.stringify(this), // Not sure what you are trying to do here
contentType: "application/json",
success: function(response, status, XHR) {
var calObj = [];
$.each(response, function (index, item) {
var evt = {
title: item.title,
start: item.startDateTime,
end: item.endDateTime
};
calObj.push(evt);
});
// You have to execute callback that is provided in the arguments
// with your events data
callback(calObj);
}
});
};
从示例中,我看到它们使用来自URL或XML文件的JSON提要,但我只需要在上述函数中创建的javascript对象
var calendar = $('#calendar').fullCalendar({
events: {
//What do I put here?
}
谢谢 根据文档
事件可以指定为一个函数:
function callEvents(response) {
var calObj = [];
$.each(response, function (index, item) {
var evt = {
title: item.title,
start: item.startDateTime,
end: item.endDateTime
};
calObj.push(evt);
});
return calObj;
//this writes out exactly what I need to go into my calendar
console.log(calObj);
};
var calendar = $('#calendar').fullCalendar({
events: getCalendarEvents
...
var getCalendarEvents = function(start, end, timezone, callback) {
$.ajax("/api/exams/", {
type: "GET",
data: JSON.stringify(this), // Not sure what you are trying to do here
contentType: "application/json",
success: function(response, status, XHR) {
var calObj = [];
$.each(response, function (index, item) {
var evt = {
title: item.title,
start: item.startDateTime,
end: item.endDateTime
};
calObj.push(evt);
});
// You have to execute callback that is provided in the arguments
// with your events data
callback(calObj);
}
});
};