关于asp.net:使用jQuery.ajax()时如何处理错误?

关于asp.net:使用jQuery.ajax()时如何处理错误?

How would you handle errors when using jQuery.ajax()?

使用jQuery的ajax方法提交表单数据时,处理错误的最佳方法是什么?
这是呼叫可能的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$.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导致statusTexterrorThrown

我可以进入XMLHttpRequest.responseText,其中包含构成标准.net错误页面的HTML。

我在responseText中找到页面标题,并使用标题来计算抛出了哪个错误。 尽管我怀疑启用自定义错误处理页面时会崩溃。

我应该在ashx中抛出错误,还是应该返回状态代码作为对userCreation.ashx调用返回的数据的一部分,然后使用它来决定要采取什么措施?
您如何处理这些情况?


为了进行调试,我通常只在页面上创建一个元素(在以下情况下:),然后将XmlHttpRequest写入该元素:

1
2
3
4
error: function (XMLHttpRequest, textStatus, errorThrown) {
    $("#error").html(XMLHttpRequest.status +"\
<hr />" + XMLHttpRequest.responseText);
}

然后,您可以查看发生的错误的类型并正确捕获它们:

1
2
if (XMLHttpRequest.status === 404) // display some page not found error
if (XMLHttpRequest.status === 500) // display some server error

您可以在ashx中抛出新的异常(例如"无效用户"等),然后将其解析出XMLHttpRequest.responseText吗?对我来说,当我遇到错误XMLHttpRequest.responseText不是标准的Asp.Net错误页面时,它是一个包含以下错误的JSON对象:

1
2
3
4
5
6
7
8
9
10
11
{
"Message":"Index was out of range. Must be non-negative and less than the size of the collection.\
\

Parameter name: index",
"StackTrace":" at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)\
\
 
at etc...",
"ExceptionType":"System.ArgumentOutOfRangeException"
}

编辑:这可能是因为我正在调用的函数已标记有以下属性:

1
2
<WebMethod()> _
<ScriptMethod()> _

Should I be throwing the errors in the
ashx, or should I be returning a
status code as part of the data
returned by the call to
userCreation.ashx, then using this to
decide what action to take? How do you
handle these situations?

就个人而言,如果可能的话,我希望在服务器端进行处理,并向那里的用户发送一条消息。在您只想向用户显示消息告诉他们发生了什么的情况下(本质上是验证消息),这种方法非常有效。

但是,如果要基于服务器上发生的情况执行操作,则可能需要使用状态代码并编写一些JavaScript来基于该状态代码执行各种操作。


现在我有一个问题,要接受哪个答案。

对问题的进一步思考使我得出结论,我错误地抛出了异常。在注册过程中,重复的用户名,电子邮件地址等是预期的问题,因此不是例外,而仅仅是错误。在这种情况下,我可能不应该抛出异常,而是返回错误代码。

这使我认为irobinson的方法应该是这种情况下的方法,尤其是因为表单只是所显示的UI的一小部分。现在,我已经实现了该解决方案,并且正在返回包含状态和要显示的可选消息的xml。然后,我可以使用jQuery对其进行解析并采取适当的措施:-

1
2
3
4
5
6
7
8
9
10
11
success: function(data){
    var created = $("result", data).attr("success");
    if (created =="OK"){
        resetNewUserForm();
        listUsers('');
    } else {
        var errorMessage = $("result", data).attr("message");
        $("#newUserErrorMessage").text(errorMessage).show();
    }
    enableNewUserForm();
}

但是travis的答案非常详细,在调试过程中或者如果我想向用户显示异常消息时将是完美的。我绝对没有收到JSON,因此它可能归因于travis列出的那些属性之一,因为我的代码中没有这些属性。

(我将接受irobinson的答案,但赞成travis的答案。接受一个票数最多的答案感觉很奇怪。)


推荐阅读