2012-03-01 65 views
2

我使用jQuery 1.2.6(遺留)。jQuery ajax不會觸發404上的ajaxStop錯誤

在我一般配置我已經設置這些選項:

jQuery(document).ajaxStart(blockUI).ajaxStop(jQuery.unblockUI); 

我有一個AJAX功能得到一個javascript文件:

function initWebtrends() { 
    console.debug("initWebtrends start");  

    var options = { 
     url : "ajax/myjavascript.js", 
     success: function(data) {     
      console.debug("webtrends integration successfully done..."); 
     }, 
     error:function(msg) { 
      console.debug("error contacting webtrends client component...");     
     } 
    }; 

    jQuery.ajax(options); 

    console.debug("initWebtrends stop"); 
} 

所有的偉大工程時,AJAX正確得到響應:在ajaxStart和ajaxStop事件被觸發。 但是當我得到一個404錯誤時,錯誤回調函數既不會被調用,也不會被調用ajaxStop事件:在這種情況下,我沒有收到任何錯誤,但是由於ajaxStart被觸發並且blockUI函數被執行,我的頁面仍然凍結。

有沒有辦法處理這種情況? 我知道在jQuery 1.5中有statusCode選項,但我必須使它在我的舊版本上工作。

親切的問候

馬西莫

+0

在你的版本是有可能使用的成功:,錯誤:功能 – 2012-03-01 10:40:54

+0

這是真實的,但這裏所描述的誤差函數不與404錯誤稱爲http://forum.jquery.com/topic/jquery -ajax-with-datatype -jsonp-will-use-error-callback-if-request-failures – 2012-03-01 14:42:17

回答

1

as pointed out in the comments by @Massimo Ugues: statuscode is not present in jQuery 1.2.6. It's present on jquery >1.5


使用statusCode(jQuery中存在1.5+)

$.ajax({ 
    statusCode: { 
    404: function() { 
     alert('page not found'); 
    } 
    } 
}); 

也可以採取statusCodeajaxSetup

$.ajaxSetup({ 
    statusCode: { 
     404: function() { 
      alert('page not found'); 
     } 
     }  
    }); 
+0

statuscode在jQuery 1.2.6中不存在。它出現在jquery> 1.5。 – 2012-03-01 14:42:41

+0

我不好,我應該更仔細地閱讀這個問題 – Rafay 2012-03-01 14:57:19

+0

接受的解決方案是爲jQuery> 1.5 – 2012-03-15 16:39:58