當使用jQuery的ajax method來提交表單數據時,處理錯誤的最佳方法是什麼? 這是一個什麼樣的通話可能看起來像一個例子:如何使用jQuery.ajax()處理錯誤?
$.ajax({
url: "userCreation.ashx",
data: { u:userName, p:password, e:email },
type: "POST",
beforeSend: function(){disableSubmitButton();},
complete: function(){enableSubmitButton();},
error: function(xhr, statusText, errorThrown){
// Work out what the error was and display the appropriate message
},
success: function(data){
displayUserCreatedMessage();
refreshUserList();
}
});
可能會失敗有很多原因,如重複的用戶名,重複的電子郵件地址等,和ashx的請求被寫入拋出一個異常當這個情況發生時。
我的問題似乎是通過拋出異常,ashx導致statusText
和errorThrown
爲undefined。
我可以到達XMLHttpRequest.responseText
,其中包含構成標準.net錯誤頁面的HTML。
我在responseText中找到頁面標題,並使用標題找出引發了哪個錯誤。雖然我懷疑當我啓用自定義錯誤處理頁面時,這會崩潰。
我應該在ashx中拋出錯誤,還是應該返回狀態碼作爲userCreation.ashx
調用返回的數據的一部分,然後使用它來決定採取何種操作?
你如何處理這些情況?
+1:非常感謝。 – 2010-08-16 00:29:58