2
我有一個網頁,調用服務器上的長時間請求。該請求生成一個excel文件,並在準備就緒時將其返回客戶端。並行表單提交和阿賈克斯調用
通過使用jQuery創建表單元素並調用submit方法來調用請求。
我想在請求過程中正在處理顯示用戶進度的任務。
我認爲這樣做使用jQuery ajax調用服務我在服務器上返回狀態消息。
我的問題是,當我打電話給這個服務(使用$ .ajax)只有當表單提交的請求結束時纔會調用回調。
有什麼建議嗎?
代碼:
<script>
function dummyFunction(){
var notificationContextId = "someid";
var url = $fdbUI.config.baseUrl() + "/Promis/GenerateExcel.aspx";
var $form = $('<form action="' + url + '" method="POST" target="_blank"></form>');
var $hidden = $("<input type='hidden' name='viewModel'/>");
$hidden.val(self.toJSON());
$hidden.appendTo($form);
var $contextId = new $("<input type='hidden' name='notifyContextId'/>").val(notificationContextId);
$contextId.appendTo($form);
$('body').append($form);
self.progressMessages([]);
$fdbUI.notificationHelper.getNotifications(notificationContextId, function (message) {
var messageText = '';
if (message.IsEnded) {
messageText = "Excel is ready to download";
} else if (message.IsError) {
messageText = "An error occured while preparing excel file. Please try again...";
} else {
messageText = message.NotifyData;
}
self.progressMessages.push(messageText);
});
$form.submit();
}
<script>
的代碼使用調用$就實用程序庫。它的代碼是:
(函數(){ 如果(window.flowdbUI){ 擲( 「失蹤參考flowdb.ui.core」);}
function NotificationHelper() {
var self = this;
this.intervalId = null;
this.getNotifications = function (contextId, fnCallback) {
if ($.isFunction(fnCallback) == false)
return;
self.intervalId = setInterval(function() {
self._startNotificationPolling(contextId, fnCallback);
}, 500);
};
this._startNotificationPolling = function (contextId, fnCallback) {
if (self._processing)
return;
self._processing = true;
self._notificationPolling(contextId, function (result) {
if (result.success) {
var message = result.retVal;
if (message == null)
return;
if (message.IsEnded || message.IsError) {
clearInterval(self.intervalId);
}
fnCallback(message);
} else {
clearInterval(self.intervalId);
fnCallback({NotifyData:null, IsEnded:false, IsError:true});
}
self._processing = false;
});
};
this._notificationPolling = function (contextId, fnCallback) {
$fdbUI.core.executeAjax("NotificationProvider", { id: contextId }, function(result) {
fnCallback(result);
});
};
return this;
}
window.flowdbUI.notificationHelper = new NotificationHelper();
})();
謝謝。我已經禁用服務頁面上的會話狀態,該服務頁面不必訪問會話,現在服務器和客戶端之間的消息傳遞工作正常。 –