且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在客户端一般处理AJAX调用错误

更新时间:2022-10-15 14:18:47

对于标准的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.