9

Im currently trying out the ajax feature, and im curious about how can i display the error messages occured when i do the ajax stuff ? For example, i have a primefaces button :

<p:commandButton value="Refresh" update="debugPanel messages" 
   action="#{checkbocLabBean.submit}"/>

When testing that button, nothing seems to happen, until i check the server logs, there's an exception. It turns out that i have a typo. Should be #{checkboxLabBean.submit}.

The only thing i can think of right now is to have disable the ajax, adding ajax="false" in my case, and the error will show up.

Is there any other ways to show errors right into the browser in development phase when using ajax requests ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bertie
  • 17,277
  • 45
  • 129
  • 182

1 Answers1

6

Ajax requests happens asynchronously. They do by default not affect the entire document. Exceptions thrown during PrimeFaces' ajax requests are delegated to <p:ajaxStatus>. You can do your thing in <facet name="error">.

<p:ajaxStatus>
    <f:facet name="start">Loading...</f:facet>
    <f:facet name="success"><h:outputText value="" /></f:facet>
    <f:facet name="error">An error has occurred!</f:facet>
</p:ajaxStatus>

If you rather want to replace the current document with the error page -which is a very reasonable choice- then it's good to know that PrimeFaces uses jQuery under the covers and that you can configure jQuery as follows to do so:

<script>
    jQuery.ajaxSetup({
        error: handleXhrError
    });

    function handleXhrError(xhr) {
        document.open();
        document.write(xhr.responseText);
        document.close();
    }
</script>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I read about ajaxStatus before, but i forgot about it, haha. Very nice handling with jquery. Im not into jquery yet, but the code is very readable =) Thank you ! – Bertie Dec 24 '10 at 04:15