使用jQuery.ajax()的时候,你会如何处理错误? [英] How would you handle errors when using jQuery.ajax()?

查看:122
本文介绍了使用jQuery.ajax()的时候,你会如何处理错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 jQuery的的的 AJAX方法提交表单数据,什么是处理错误的最好方法是什么? 这是一个什么样的电话可能看起来像一个例子:

  $。阿贾克斯({
    网址:userCreation.ashx
    数据:【U:用户名,电话号码:密码,电子邮件:电子邮件},
    键入:POST,
    beforeSend:功能(){disableSubmitButton();},
    完成:函数(){enableSubmitButton();},
    错误:函数(XHR,状态文本,errorThrown){
            //制定出什么错误了,并显示相应的消息
        },
    成功:功能(数据){
            displayUserCreatedMessage();
            refreshUserList();
        }
});
 

的请求可能为多种原因,诸如重复的用户名失败,重复的电子邮件地址等,且ashx的写入时发生这种情况,以抛出异常。

我的问题似乎是,通过抛出异常的ashx的导致状态文本和errorThrown是不确定。我可以给XMLHtt prequest.responseText其中包含构成了标准的.NET错误页面的HTML。

我发现在responseText的页面标题和使用的标题制定哪些错误被抛出。虽然我怀疑这会崩溃,当我启用自定义错误处理页面。

我应该扔了错误的ASHX,或者我应该返回一个状态code通过调用userCreation.ashx返回的数据的一部分,然后用这个来决定采取什么行动​​?你如何处理这些情况?

解决方案
  

我应该扔了错误   ASHX,或者我应该返回   状态code作为数据的一部分   通过调用返回   userCreation.ashx,然后用这   决定采取什么行动​​?你怎么   处理这些情况?

就个人而言,如果可能的话,我会preFER来处理这个服务器端和邮件工作提高到用户那里。这个工程相当不错,在这样一个场景,你只想显示一条消息,告诉他们发生了什么(验证消息,本质上)用户。

不过,如果要执行的基础上发生的事情在服务器上的操作,您可能需要使用状态code,写一些JavaScript来执行基于该状态code的各种动作。

When using jQuery's ajax method to submit form data, what is the best way to handle errors? This is an example of what a call might look like:

$.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();
        }
});

The request might fail for a number of reasons, such as duplicate user name, duplicate email address etc, and the ashx is written to throw an exception when this happens.

My problem seems to be that by throwing an exception the ashx causes the statusText and errorThrown to be undefined. I can get to the XMLHttpRequest.responseText which contains the HTML that makes up the standard .net error page.

I am finding the page title in the responseText and using the title to work out which error was thrown. Although I have a suspicion that this will fall apart when I enable custom error handling pages.

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?

解决方案

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?

Personally, if possible, I would prefer to handle this on the server side and work up a message to the user there. This works very well in a scenario where you only want to display a message to the user telling them what happened (validation message, essentially).

However, if you want to perform an action based on what happened on the server, you may want to use a status code and write some javascript to perform various actions based on that status code.

这篇关于使用jQuery.ajax()的时候,你会如何处理错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆