2014-10-01 87 views
0

我已經在服務器端設置的Ajax錯誤處理程序在客戶端ajaxError調用的語法錯誤

$(document).ajaxError(processAjaxError); 
$.getJSON('/data.json'); 

def get(self): 
    self.response.headers['Content-Type'] = 'application/json' 
    self.response.write('Hello, World!') 

即使我得到200狀態碼,仍然ajaxError回調越來越因爲響應數據不是json。但在processAjaxError回調參數中,我如何得到確切的錯誤信息?

+0

功能processAjaxError(事件,jqxhr,設置,thrownError){...這裏檢查thrownError} – SSA 2014-10-01 11:20:54

+0

感謝。我使用這個參數。我在哪裏可以看到確切的錯誤信息。 – 2014-10-01 11:21:47

+1

@SSA我認爲這返回HTTP狀態描述,所以不會給JavaScript錯誤,因爲狀態代碼是200 - 確定「 – Rhumborl 2014-10-01 11:22:15

回答

0

的ajaxerror函數應該按以下順序接收參數:

event, jqxhr, settings, thrownError 

此處瞭解詳情:http://api.jquery.com/ajaxerror/

從文檔完整的示例:

$(document).ajaxError(function(event, jqxhr, settings, thrownError) { 
    if (settings.url == "ajax/missing.html") { 
    $("div.log").text("Triggered ajaxError handler."); 
    // Do whatever you want with thrownError here 
    } 
}); 

如果你想圖出爲什麼 404被拋出(如果它是無效的JSON或其他原因),你可以嘗試解析的責任你回來的數據的seText排除了這種可能性。

try { 
    $.parseJSON(responseText); 
} catch(err) { 
    // Failed the json test 
} 
+0

謝謝。這是我使用的代碼。但真正的問題是沒有丟失頁面(404)和其他的http 400或500狀態代碼。我得到200的響應,但響應是不是一個有效的json,這裏的ajaxError回調函數沒有關於它是語法錯誤還是其他的細節 – 2014-10-01 11:27:31

+0

在我的答案中增加了更多內容,希望對你有所幫助 – 2014-10-01 11:37:44

+0

謝謝,但我真的不知道,語法錯誤唯一可能? – 2014-10-01 12:52:18