JSF2.0 - 使用Primefaces 3.0处理错误的Ajax调用

时间:2022-03-07 20:03:17

I have a commandButton in my .xhtml page:

我的.xhtml页面中有一个commandButton:

<p:commandButton action="#{someone.doSomething()}"
                ajax="true"
                onerror="errorDialog.show();">
</p:commandButton>

It's simply making an Ajax call. How can I detect situations such as internet connection problem (of the client/browser), timeout, session-timeout, server-side exceptions, crashes etc in the middle of an Ajax call so as to show an informative message to the user?

它只是在进行Ajax调用。如何在Ajax调用过程中检测诸如(客户端/浏览器的)因特网连接问题,超时,会话超时,服务器端异常,崩溃等情况,以便向用户显示信息性消息?

Does the onerror attribute of p:ajax handle all of those? If not, then what? :) What's the default timeout btw?

p:ajax的onerror属性是否处理所有这些?如果没有,那又怎样? :)什么是默认超时btw?

Any help appreciated, thanks.

任何帮助表示感谢,谢谢。

1 个解决方案

#1


2  

onerror calls this function:
onerror(xhr, status, exception) - Javascript callback to process when ajax request fails. Takes three arguments, xmlhttprequest, status string and exception thrown if any.
This info is from documentation. xhr - is actually a request. So there can be found request status and a lot of other info.

onerror调用此函数:onerror(xhr,status,exception) - 当ajax请求失败时进行Javascript回调处理。采用三个参数,xmlhttprequest,状态字符串和抛出的异常(如果有的话)。此信息来自文档。 xhr - 实际上是一个请求。因此可以找到请求状态和许多其他信息。

<p:commandButton action="#{someone.doSomething()}"
            ajax="true"
            onerror="console.debug(xhr)">
</p:commandButton>

Try this code in chrome or firebug. It will show xhr object

在chrome或firebug中尝试此代码。它将显示xhr对象

Get a look into http://primefaces.googlecode.com/files/primefaces_users_guide_3_0.pdf section 7.2 Ajax API

查看http://primefaces.googlecode.com/files/primefaces_users_guide_3_0.pdf第7.2节Ajax API

#1


2  

onerror calls this function:
onerror(xhr, status, exception) - Javascript callback to process when ajax request fails. Takes three arguments, xmlhttprequest, status string and exception thrown if any.
This info is from documentation. xhr - is actually a request. So there can be found request status and a lot of other info.

onerror调用此函数:onerror(xhr,status,exception) - 当ajax请求失败时进行Javascript回调处理。采用三个参数,xmlhttprequest,状态字符串和抛出的异常(如果有的话)。此信息来自文档。 xhr - 实际上是一个请求。因此可以找到请求状态和许多其他信息。

<p:commandButton action="#{someone.doSomething()}"
            ajax="true"
            onerror="console.debug(xhr)">
</p:commandButton>

Try this code in chrome or firebug. It will show xhr object

在chrome或firebug中尝试此代码。它将显示xhr对象

Get a look into http://primefaces.googlecode.com/files/primefaces_users_guide_3_0.pdf section 7.2 Ajax API

查看http://primefaces.googlecode.com/files/primefaces_users_guide_3_0.pdf第7.2节Ajax API