当使用jQuery的ajax method提交表单数据时,处理错误的最佳方式是什么? 以下是呼叫可能外观的示例:
$.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
,其中包含构成标准的HTML.网络错误页面.
我在responseText中查找页面标题,并使用该标题找出抛出了哪个错误.虽然我怀疑当我启用自定义错误处理页时,这会崩溃.
我应该在ashx中抛出错误,还是应该将状态代码作为调用userCreation.ashx
返回的数据的一部分返回,然后使用它来决定采取什么操作?
你是如何处理这些情况的?