[英]Why is ajaxStop() not firing in Internet Explorer?
這是我第一次使用ajax。 我不知道ajaxStop發生在哪里。 我使用ajaxStart來顯示加載圖像,需要ajaxStop來隱藏加載圖像。 請幫忙。
我有這個代碼從“PageOne”調用彈出窗口
function ShowFixSteps(path, title){
var winHeight = parseInt(jQuery(window).height() - 100);
var winWidth = parseInt(jQuery(window).width() - 600);
jQuery.ajax({
url: path,
success: function(data) {
jQuery("#divPopup").load(path).dialog({
modal: true,
width: winWidth,
height: winHeight,
title: title,
position: "center"
});
}
});
jQuery("#divPopup").bind("dialogbeforeclose", function(){
jQuery("#divPopup").empty('');
});
}
在我的母版頁面上,我有這個代碼來檢查ajax調用的開始和停止:
$(document).ajaxStart(function() {
alert('start');
});
$(document).ajaxStop(function() {
alert('stop');
});
$(document).ajaxError(function() {
alert('error');
});
它會警告START但不會發出STOP:也沒有錯誤。
注意:START和STOP警報適用於Chrome,但不適用於IE。
在所有當前的AJAX請求完成后觸發ajaxStop
。
您可以使用jQuery API文檔閱讀有關ajaxStop
更多信息。
您可以通過以下方式使用.ajaxStop()
:
$(document).ajaxStop(function() {
$('#loading-spinner').hide();
});
或者您可以添加:complete
對AJAX函數的回調,如下所示:
jQuery.ajax({
url: path,
success: function(data) {
jQuery("#divPopup").load(path).dialog({
modal: true,
width: winWidth,
height: winHeight,
title: title,
position: "center"
});
},
complete: function() {
// do something here when ajax stops
// like hiding the spinner or calling another function
}
});
當你提到如何在你的一條評論中停止一個AJAX請求時,方法如下:
var ajax1 = $.ajax({
type: "POST",
url: "some.php",
...
});
ajax1.abort()
您可以通過執行以下操作來檢查在中止之前是否正在運行特定的AJAX請求:
if (ajax1) {
ajax1.abort();
}
或者您可以通過執行以下操作來檢查是否有任何 ajax請求在中止之前運行:
var ajax_inprocess = false;
$(document).ajaxStart(function() {
ajax_inprocess = true;
});
$(document).ajaxStop(function() {
ajax_inprocess = false;
});
if (ajax_inprocess == true) {
request.abort();
}
請注意使用.abort()
,因為它只會阻止客戶端代碼偵聽響應,它實際上不會阻止服務器工作。 實際上有一些主要的注意事項,所以請務必先閱讀。
對更新問題的更新答案
對於IE問題,請嘗試使用:
$(document).ajaxComplete(function() {
// do something
})
而不是ajaxStop()
。 ajaxComplete()
將在每次AJAX請求完成時觸發,而不是在所有請求都使用ajaxStop()
完成時ajaxStop()
。 也許它會有所幫助,也許不會。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.