2014-04-28 36 views
1

當我使用fetch()方法獲取結果時,如何獲得骨幹js中的響應代碼? 我知道它在響應代碼類似4xx時執行錯誤回調函數,當響應代碼類似2xx時,它執行成功回調方法。如何獲得主幹js的響應代碼?

var x = user.fetch({ 
    error: function(model, xhr, options){ 
     alert('Error on fetch') 
     console.log(xhr.responseText); 
    }, 
    success: function(model, response, options) { 
     alert(user.toJSON()); 
    } 

在這裏我想獲得響應代碼,無論它是一個錯誤的回調或成功的回調。

在此先感謝。

+0

什麼是你從你的提醒/控制檯獲得。在這裏登錄命令? – Lix

+0

感謝Lix的快速回復。 如果錯誤console.log(xhr.responseText); 顯示錯誤消息,我在響應對象中設置了服務器端。 並且在成功的情況下console.log(user.toJSON());顯示模型中可用的數據。 –

回答

4

如果你想響應代碼得到這樣的:成功的 - 在情況下,

console.log("Response code in case of success:",options.xhr.status); 

- 在案件的錯誤,

console.log("Response code in case of failure",xhr.status); 
+0

謝謝潘。這就是我正在尋找的東西。它完美的作品。 –