Search code examples
ajaxjsfonerror

<f:ajax onerror> called on every ajax request even if completed


This simple piece of code makes an alert every time a change event happens in the spinner. However the ajax status says the request has been completed and doesn't notify an error.

<p:ajaxStatus/>   
<p:spinner value="#{testBean.number}"
    id="testSpinner" min="1" max="20">
    <f:ajax event="change" onerror="alert('error')"/>
</p:spinner>

The content of ajax status is this one :

<f:facet name="default">
    <h:outputText value="Status: StandBy" />
</f:facet>

<f:facet name="onerror">
    <h:outputText value="Status: onerror" />
</f:facet>

<f:facet name="complete">
    <h:outputText value="Status: Completed" />
</f:facet>

Solution

  • You have to write something that return a function type for the onerror attribute. If you write a function call then this function is called in order to obtain a value, if this value was a function type there would be no problem, but alert function doesn't return any function type, moreover it generate the side efect of printing a message on the screen. So what you have to do is:

    define the myfunction function:

    <script>
        var myfunction = function(){alert('error')};
    </script>
    

    and then do:

    <f:ajax event="change" onerror="myfunction"/>
    

    NOTE:

    Beware of not mixing up <f:ajax ... /> and <p:ajax ... />. These two are equivalent:

    <f:ajax onerror="function(){alert('ERROR')}" />
    

    and

    <p:ajax onerror="alert('ERROR')" />