2 Replies Latest reply on Nov 14, 2014 11:41 AM by keithdmoore94

    Seam web remoting not working within a javascript funtion

    enikozsido

      Hi all,


      Please take a look at following source!


      The interface is:


      @Local
      public interface IMessageServices
      {
           @WebRemote
           public String addMessage(String key);
      
      }




      then the implementation:


      @Scope(ScopeType.SESSION)
      @Name("messageService")
      public class MessageService implements IMessageServices
      {
           @WebRemote
           public String addMessage(String key)
           {
                FacesMessages.instance().addFromResourceBundle(FacesMessage.SEVERITY_ERROR, key);
                return "success";
           }
      }
      




      the xhtml :


      ...
           <script type="text/javascript" src="seam/resource/remoting/resource/remote.js"></script>
           <script type="text/javascript" src="seam/resource/remoting/interface.js?messageService"></script>
      ...
               <script type="text/javascript">
                     //<![CDATA[
      ...
                 Seam.Remoting.setDebug(true);            
                   var str = "message"
                   Seam.Component.getInstance("messageService").addMessage(str,success);
                   
                      function success(result){
                     alert(result);
                }
                     //]]>
           </script>
      



      The debug info is:


      Fri Aug 01 2008 15:27:52 GMT+0300: Request packet:
      <envelope><header><context></context></header><body><call component="messageService" method="addMessage" id="0">
      <params><param><str>message</str></param></params><refs></refs></call></body></envelope>
      
      
      Fri Aug 01 2008 15:27:52 GMT+0300: Response packet:
      <envelope><header><context><conversationId>27</conversationId></context></header><body><result id="0"><value><str>success</str></value><refs></refs></result></body></envelope>
      




      This works as expected: alerts(succeess).



      I'm trying to remotely invoke my method inside a javascript function:



       function addMessage(str){
                   Seam.Component.getInstance("messageService").addMessage(str,success);
       }
      ....
      <h:commandButton  value="Add" onclick="addMessage('message');"/>
      

                
      This time I get an error alert There was an error processing your request.  Error code: 0.
      The Debug info is:


      Fri Aug 01 2008 15:40:49 GMT+0300: Request packet:
      <envelope><header><context></context></header><body><call component="messageService" method="addMessage" id="0">
      <params><param><str>message</str></param></params><refs></refs></call></body></envelope>
      



      As you can see there is no response.
      Do you have any idea why this happens?


        • 1. Re: Seam web remoting not working within a javascript funtion
          teacurran
          I know this is an old post but I was having this exact problem and I wanted to post for anyone else having this issue.

          The problem is that <h:commandButton is causing a form submit to happen immediately after the ajax request is queued to happen.

          If you change it to something like <a href="javascript:addMessage('message')">click</a> the problem goes away.
          • 2. Re: Seam web remoting not working within a javascript funtion
            keithdmoore94

            Just posting this in hopes of helping others.  I had this problem and I needed to invoke a callback to issue a window.close().

             

            Before:

             

            function doSomething() {

                 var action = getComponent("myAction");  // This function gets the component in the usual way...

                 action.doSomeWork();

                 window.close();
            }

             

            After:

            function doSomething() {

                 var action = getComponent("myAction");  // This function gets the component in the usual way...

                 action.doSomeWork(function() {

                   window.close();

                 });

            }