2009-12-20 43 views
1

我在我的代碼中執行「$ .ajax」調用的許多地方 我可以定義一個地方來處理所有錯誤嗎?jquery ajax error

我知道有一個「錯誤:函數...」我可以在每個呼叫的地方,但是我想寫它只是一個時間

回答

1
$.ajaxSetup({}); 
+3

非常非常...簡潔。 @OP:它爲Ajax請求設置默認選項。鏈接:http://docs.jquery.com/Ajax/jQuery.ajaxSetup#options – 2009-12-20 15:23:39

+0

你有一個示例代碼? – haddar 2009-12-20 15:28:41

+0

$ .ajaxSetup({ 錯誤:函數(XMLHttpRequest參數url errorThrown){ 警報(XMLHttpRequest的+ ':' + textStatus + ':' + errorThrown); 將返回false;} }); 是可以嗎? – haddar 2009-12-20 15:34:25

0

您可以只定義一次錯誤處理函數,然後在Ajax調用中引用它,而不是在每個調用中使用函數文字。

1

你應該使用$ .ajaxSetup()事件處理方法(由JQuery的文檔說明):

http://api.jquery.com/jQuery.ajaxSetup/

Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().

使用$ .ajaxError()方法相反:

http://api.jquery.com/ajaxError