title: Grails - how to return a variable from controller's action to JavaScript?
body: <p>I'm trying to return a result from controller's action method to JavaScript function.</p>
  
  <p>I tried two ways of solving this:</p>
  
  <p>first:</p>
  
  <p>Method in a controller:</p>
  
  <pre><code>def addUser(String name, String id1, String id2) {
      return "myValue"
  }
  </code></pre>
  
  <p>JavaScript function:</p>
  
  <pre><code>function myFunction(userId1, userId2, username) {
    var id1 = $(userId1).val();
    var id2 = $(userId2).val();
    var name = $(username).val();
   // &lt;g:remoteFunction action="addUser" params="{name:name,id1:id1,id2:id2}" /&gt;
    var variable = ${remoteFunction(action:'addUser', params:[name:name,id1:id1,id2:id2])};
    alert(variable)
  }
  </code></pre>
  
  <p>It only displayed "object Object"</p>
  
  <p>second:</p>
  
  <p>Method in a controller:</p>
  
  <pre><code>def addUser(String name, String id1, String id2) {
      return [value:"myValue"]
  }
  </code></pre>
  
  <p>JavaScript function:</p>
  
  <pre><code>function myFunction(userId1, userId2, username) {
    var id1 = $(userId1).val();
    var id2 = $(userId2).val();
    var name = $(username).val();
   // &lt;g:remoteFunction action="addUser" params="{name:name,id1:id1,id2:id2}" /&gt;
    ${remoteFunction(action:'addUser', params:[name:name,id1:id1,id2:id2])};
        var variable = "${value}";
    alert(variable)
  }
  </code></pre>
  
  <p>Doesn't display anything.</p>
  
  <p>I also tried putting my variable into the session which only worked after refreshing the page.
  I ran out of ideas. What am I missing?</p>