在客户端一般处理AJAX调用错误 [英] General handling of AJAX call error on client side
问题描述
现在,当发生错误时,我检查一个AJAX请求并提供部分响应(如 BalusC对这个问题的反应)。
但是,如果没有任何服务器来处理错误,例如由于连接丢失?目前,一切都没有发生,让用户感到困惑。
我在这个问题可以解决,但是我想以一般的方式解决这个问题,所以所有的AJAX调用都是无法尝试重定向到起始页面 - 然后可能会收到浏览器连接错误消息。
对于标准的JSF ajax,请使用 jsf.ajax。 addOnError()
设置默认错误处理程序。例如
jsf.ajax.addOnError(function(data){
alert .responseText);
});
另见 JSF 2.2规格。您可以在JSF规范的表14-4中找到 data
对象的所有属性。
对于PrimeFaces 4+ jQuery中的钩子 pfAjaxError
事件(4之前,只使用 ajaxError
)。例如
$(document).on(pfAjaxError,function(event,xhr,options){
alert (xhr.responseText);
});
只需自定义即可显示顶部的某个div。
We have a JSF 2.2 application using PrimeFaces.
Now, when an error occurs, I check for an AJAX request and deliver a partial response (as shown in BalusC's anwer to this question).
But what is, if there's no server anymore to handle the error, e.g. due to connection loss? At the moment, just nothing happens, leaving the user puzzled.
I found a hint in that question, which works, but I'd like to solve this in a general way, so that all AJAX calls which fail try to redirect to the start page - and then may receive the browser connection error message.
For standard JSF ajax, use jsf.ajax.addOnError()
to set the default error handler. E.g.
jsf.ajax.addOnError(function(data) {
alert(data.responseText);
});
See also chapter 13.3.6.2 of the JSF 2.2 spec. You can find all properties of data
object in table 14-4 of the JSF spec.
For PrimeFaces 4+, hook pfAjaxError
event in jQuery (before 4, just use ajaxError
). E.g.
$(document).on("pfAjaxError", function(event, xhr, options) {
alert(xhr.responseText);
});
Just customize it accordingly to show some div in top.
这篇关于在客户端一般处理AJAX调用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!