title: Retrieving json values from controller to ajax function
body: <p>I have this ajax code</p>
  
  <pre><code>$(document).ready(function() {
          for(var i=0;i&lt;8;i++){
          $(".exac").append("&lt;div class='ex' id='eas"+i+"' style='cursor:pointer'&gt;&lt;h1 id='h1'&gt;&lt;/h1&gt;&lt;/div&gt;");
          }
          $('.ex').click(function ()
          {
              var x=$(this).prop("id");
              $.ajax({
                  type: "POST",
                  contentType: "application/json",
                  dataType: "json",
                  url: "/random/random", 
                  success: function(msg){    
              alert(msg);  
                               if(msg==01101){
                              //$('.ex').attr('disabled','disabled');
                              $(".ex").off('click');
                              $("#hello").append("&lt;button&gt;&lt;a href='reset.jsp'&gt;Reset&lt;/a&gt;&lt;/button&gt;");
                          }
                          else{
                      $('&lt;p&gt;'+msg+'&lt;/p&gt;').appendTo("#"+x);
                      $("#"+x).off('click');
                     }
                 }
              });
          });
          $('#button').removeAttr('disabled');
      });
  </code></pre>
  
  <p>and this is my controler</p>
  
  <pre><code>class RandomController &lt; ApplicationController
  
  def random
  @n = 8
      respond_to do |format|
        #format.json { render :json =&gt; index }
        format.json{render :json =&gt; ActiveSupport::JSON.encode( @n )}
          format.html { redirect_to action: "index" }
  
      end
  end
  
  end
  </code></pre>
  
  <p>I am entering the controller using the ajax option and also I get redirected back to the index page but I am not able to recieve the value of <code>@n=8</code> back at the index page.Am I missing some step? </p>
  
  <p>This is the error I get in my console</p>
  
  <p><img src="https://i.stack.imgur.com/tXtC8.png" alt="enter image description here"></p>