5 Replies Latest reply on Jan 21, 2011 7:07 AM by muralib12

    How to cancel an Ajax request?

    lowecg2004

      In RF 3.0.1 I was able to cancel an ajax request by returning false from the JavaScript function called from onsubmit. The following code snippet calls confirmChangeLoss() which returns true to proceed with the ajax call or false if it should be ignored. In RF 3.1.0, this is no longer works - if I return false, the ajax call happens ayway. Should it be working? Or is there now a different mechanism for canceling requests?

      Best Regards,

      Chris.


      <h:selectOneMenu value="#{permissionsHome.selectedGroup}" required="true" >
       <a4j:support event="onchange" action="#{permissionsHome.onChangeGroup}" onsubmit="confirmChangeLoss()" oncomplete="resetChanged()" reRender="grd-permission"/>
       <s:selectItems value="#{groups}" var="group" label="#{group.description}" />
       <s:convertEntity />
       </h:selectOneMenu>
      
      The corresponding JavaScript
      
      var stateModified = false;
      
      function confirmChangeLoss() {
       var result = true;
      
       if (stateModified) {
       result = confirm('Lose unsaved changes?');
       }
      
       return result;
      }



        • 1. Re: How to cancel an Ajax request?
          ilya_shaikovsky
          • 2. Re: How to cancel an Ajax request?
            lowecg2004

            Thanks for looking at this, I've added my vote the the case.

            Cheers,

            Chris.

            • 3. Re: How to cancel an Ajax request?
              ilya_shaikovsky

              I'll cancel the bug. We both have made the same mistake from the begining :)

               <h:form>
               <h:inputText value="#{bean.property}">
               <a4j:support event="onkeyup" reRender="test"
               onsubmit="if (confirm('Lose unsaved changes?')) {return false;}">
               </a4j:support>
               </h:inputText>
               <h:outputText value="#{bean.property}" id="test"></h:outputText>
               </h:form>
              


              This code works. So you should add this additional "if" statement to check what was returned from function.

              • 4. Re: How to cancel an Ajax request?
                lowecg2004

                D'oh - you're right! Sorry to have wasted your time.

                Chris.

                • 5. Re: How to cancel an Ajax request?
                  muralib12

                  Hi llya,

                   

                  I am using richfaces-ui-3.3.2.GA.jar and facing strange scenario... I have JSF code just like below

                   

                   

                  <a4j:form ajaxSubmit="true" >                    
                  
                      <h:inputText id="seekIndex" size="1" maxlength="3" value="#{searchVo.index}">
                  
                          <a4j:support event="onkeyup" action="goto" 
                               onsubmit="if (event.keyCode != 13) { return false; }else if(!navigateAway()){ return false; } "
                                   reRender="xyzFragment"/> 
                      </h:inputText> of #{searchVo.count}
                  
                  </a4j:form>    
                  

                   

                  It works fine with the context name like 'myproject'. if context name has some special characters(-)  like 'myproject-test' this throws 404 error when it submits ajax request.

                   

                   

                  <form id="j_id98" name="j_id98" method="post" 
                  action="javascript:A4J.AJAX.SubmitForm('j_id98',{'similarityGroupingId':'j_id98','parameters':{'j_id98':'j_id98'}%20,
                  'actionUrl':'/myproject/x2Dtest/spring/myInformation?execution=e1s1'}%20)" target="">
                  
                  <input type="text" id="j_id98:seekIndex" name="j_id98:seekIndex" value="1" maxlength="3" onkeyup="if (event.keyCode != 13) { return false; }else if(!navigateAway()){ return false; } ;
                  A4J.AJAX.Submit('j_id98',event,{'similarityGroupingId':'j_id98:j_id99','parameters':{'j_id98:j_id99':'j_id98:j_id99'} ,
                  'actionUrl':'/myproject\x2Dtest/spring/myInformation?execution=e1s1'} )" size="1" autocomplete="off"> of 5<input type="hidden" autocomplete="off" name="j_id98" value="j_id98"><input type="hidden" autocomplete="off" name="autoScroll" value="">
                  <script type="text/javascript">
                  //<![CDATA[
                  function clear_j_id98() {
                  _clearJSFFormParameters('j_id98','',['j_id98:_link_hidden_','j_id98:j_idcl']);
                  }
                  function clearFormHiddenParams_j_id98(){clear_j_id98();}
                  function clearFormHiddenParams_j_id98(){clear_j_id98();}
                  clear_j_id98();
                  //]]>
                  </script><script type="text/javascript">//<![CDATA[
                  A4J.setupForm('j_id98');
                  //]]>
                  </script><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="e1s1" autocomplete="off"></form>
                  
                  

                   

                   

                  '/myproject/x2Dtest/spring/myInformation?execution=e1s1'  obviously this is encoded wrongly.

                   

                  plz help me how resolve this??