什么是规范的方法在Ajax的请求来处理错误? [英] What is the canonical way to handle errors during Ajax-requests?
问题描述
对于普通的请求,我们可以简单的注册一个<错误页>
在的web.xml
。然而,这并不适用于Ajax的请求。通过Ajax的请求期间默认的错误将导致在一个小弹出式窗口中,显示了异常浏览器
For normal requests we can simple register an <error-page>
in web.xml
. However, this does not apply to Ajax-requests. By default errors during an Ajax-request will result in a little pop-window in the browser that shows the exception.
主要的例子中,我挣扎正在处理 ViewExpiredException
以统一的方式。对于标准的要求,我重定向到一个网页,解释说没有登录用户,并提供了一个链接到登录页面。我想这样做的AJAX请求。似乎有几种方式:
The main example I am struggling with is handling a ViewExpiredException
in a uniform way. For standard requests, I redirect to a page that explains that the user is not logged in and provides a link to the login-page. I would like to do the same for Ajax-requests. There seem to be several ways:
- 我可以写一个处理错误的客户端,并重定向到错误页面的JavaScript功能。我将不得不添加此功能的每个
&LT; F:AJAX&GT;
-tag所有页面上的使用的onerror -attribute $ C $。有没有办法告诉JSF,我想有这样的JavaScript功能作为默认的错误处理所有
&LT; F:?AJAX&GT;
标签都有效 - 我可以使用自定义异常处理程序,如<一个描述href="http://weblogs.java.net/blog/edburns/archive/2009/09/03/dealing-gracefully-viewexpiredexception-jsf2"相对=nofollow>这个博客。这似乎做我想做的,但我不知道它是否是矫枉过正。有没有简单的解决方案?
- I could write a javascript function that handles the error on the client-side and redirects to the error-page. I would then have to add this function every
<f:ajax>
-tag on all pages using theonerror
-attribute. Is there a way to tell JSF that I want to have this javascript-function as the default error-handler for all<f:ajax>
-tags? - I could use a custom exception-handler, as described in this blog. This seems to do what I want, but I wonder if it is overkill. Is there no simpler solution?
我的问题是,这是怎么应该是解决了吗?其中我列出的方法应使用?有没有我不知道的另一种方法?
So my question is, how is this supposed to be solved? Which of the approaches I listed should be used? Is there another approach that I do not know of?
推荐答案
您可以使用的 jsf.ajax.addOnError()
设置默认错误处理程序。例如,
You can use jsf.ajax.addOnError()
to set the default error handler. E.g.
jsf.ajax.addOnError(function(data) {
alert(data.responseText);
});
又见 JSF2规范章13.3.6.2。你可以找到数据的所有属性
在JSF2规范表14-4对象。
See also chapter 13.3.6.2 of the JSF2 spec. You can find all properties of data
object in table 14-4 of the JSF2 spec.
这篇关于什么是规范的方法在Ajax的请求来处理错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!