[英]Multiple ajax calls in event function full calendar
我在我們的頁面中使用 fullcalendar jQuery 插件來創建/查看會議邀請。
我的新要求是在我們的頁面中顯示在 Outlook 中為該特定用戶創建的會議。 我的網絡服務(用於從 Outlook 中提取會議)花了 45 秒的時間來發送響應。 我不希望用戶完全等待 45 秒。(性能問題)所以我只想先從 db 加載事件,然后我想附加作為 webservice response 返回的事件。 因此用戶感覺不到那么多延遲。
所以我只是進行了兩次 ajax 調用來提取所需的詳細信息。 一個ajax調用是從本地數據庫(SUCCESS)拉取事件,另一個是調用webservice拉取在Outlook中創建的事件。
events: function(start, end, timezone,callback) {
$.ajax({
url: // url hits db and gets meeting details in db
dataType: 'json',
success: function(response) {
var events = [];
if(response != null){
alert("Success");
$.map(response ,function ( r ){
alert(r.title + " " + r.start + " " + r.end);
events.push({
title : r.title,
start : r.start,
end : r.end
});
});
}
callback(events);
}
$.ajax({
url: // url calls webservice and gets meetings in Outlook
dataType: 'json',
success: function(response) {
var events = [];
if(response != null){
alert("Success");
$.map(response ,function ( r ){
alert(r.title + " " + r.start + " " + r.end);
events.push({
title : r.title,
start : r.start,
end : r.end
});
});
}
alert("External Events "+ events.length); //EXECUTED
callback(events); //NOT EXECUTED
}
});
}
現在的問題是,
1 . 第一次 ajax 調用工作正常。
2 . 我從 Webservice 得到了正確的響應,但響應沒有附加到 calendar 。
我的疑問是,
我們不能兩次使用 callback(events) 嗎?
或者請建議我對此的替代解決方案?
如果分別使用兩個事件函數,則只會執行第二個事件函數。 為什么第一個事件函數沒有被執行?
您的代碼沒有錯。 確保從服務器獲得的響應是您期望的(例如,響應!= null)。
https://jsfiddle.net/5ds8z06p/
var foo = function(callback) {
$.ajax({
url: '/echo/json',
success: function() {
callback('first');
}
});
$.ajax({
url: '/echo/json',
success: function() {
callback('second');
}
});
};
foo(function(bar) {
console.log(bar);
});
有點舊了,但是可以參考。 在第一個ajax調用中,而不是在回調中,放入第二個ajax調用
$.ajax({
url: // url hits db and gets meeting details in db
dataType: 'json',
success: function(response) {
var events = [];
if(response != null){
alert("Success");
$.map(response ,function ( r ){
alert(r.title + " " + r.start + " " + r.end);
events.push({
title : r.title,
start : r.start,
end : r.end
});
});
}
//second call
$.ajax({
url: // url calls webservice and gets meetings in Outlook
dataType: 'json',
success: function(response) {
var events = [];
if(response != null){
alert("Success");
$.map(response ,function ( r ){
alert(r.title + " " + r.start + " " + r.end);
events.push({
title : r.title,
start : r.start,
end : r.end
});
});
}
alert("External Events "+ events.length);
callback(events); // return all results
}
});
}
只是在黑暗中拍攝,但是以前使用$.ajax
從.Net WCF服務中獲取json響應中包括日期值的json時,當一個或多個日期字段設置為DateTime.MinValue
時,我已經看到了此行為。
MinValue
殺死了json序列化程序。 客戶端和服務器端的一切似乎都按預期工作,但是從未調用過“成功”回調。 填充所有日期值即可解決該問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.