title: Putting a JSON response into a hidden field and retrieving it into a function
body: <p>I'm retrieving the number of rows contained by a table in my database with the following function using JSON.</p>
  
  <pre><code>function rowCount()
  {
      $.ajax({
          headers: { 
                  'Accept': 'application/json',
                  'Content-Type': 'application/json' 
              },
          datatype:"json",
          type: "GET",                    
          url: "/wagafashion/ajax/CmsRowCount.htm",
          success: function(response)
          {
              $("#rows").val(response);
          },
          error: function(e)
          {
              alert('Error: ' + e);
          }
      });
  }            
  </code></pre>
  
  <p>In the success handler, the response is arriving as expected. There is no problem on the server side.</p>
  
  <p>The response is just mapped with the <code>long</code> type of Java which represents the number of rows in a database table.</p>
  
  <p>I'm putting this response in a hidden field whose <code>id</code> is <code>rows</code> using <code>$("#rows").val(response);</code> in the success handler.</p>
  
  <p>The above function is invoked when the form is submitted using the following jQuery function.</p>
  
  <pre><code>$(function() {
      $('#dataForm').submit(function() {
  
          rowCount();  //Invokes the above function that makes a JSON request.
          var rows=$("#rows").val();
          alert("rows = "+rows);
  
          return false;
      });
  });
  </code></pre>
  
  <p>The alert box attempts to alert the value contained by the hidden field (which is the JSON response as described above) but <strong>it is empty</strong> for the first time. It alerts the actual value only when I press the submit button once again (without a page refresh).</p>
  
  <hr>
  
  <p>Also tried to replace the preceding function with the following.</p>
  
  <pre><code>$(function() {
      $('#dataForm').submit(function() {
  
          rowCount();  //Invokes the first function that makes a JSON request.     
  
          var form = $(this),
          url = form.attr('action'),
          rows = form.find('input[name="rows"]').val();
  
          alert("rows = "+rows);
  
          return false;
      });
  });
  </code></pre>
  
  <p>But it didn't work either. Why does this happen? What is the way of retrieving the correct value of that hidden field into the preceding jQuery function?</p>