title: AJAX call and JSON data
body: <p>Good day!</p>
  
  <p>I'm learning to create AJAX calls to PHP scripts. </p>
  
  <p>Basically, I want to find the best solution to handle AJAX calls. In <a href="https://stackoverflow.com/questions/3382174/jquery-ajax-call-how-to-handle">this</a> question you can find my client-side part of the code.</p>
  
  <p>Basically, I'd like to verify my knowledge here and if I'm wrong, get any tips how to make it correct way.</p>
  
  <p>So... I'm using KohanaPHP framework (but also going to learn ZendFramework). I created a test controller with following code:</p>
  
  <pre><code>public function __construct() 
  {
      if (request::is_ajax()) {
          $this-&gt;auto_render = FALSE;
          header('content-type: application/json');
      }
  }
  
  public function index()
  {
      $result['success'] = 1;
  
      $test_model = new Test_Model;
      $items = $test_model-&gt;get_test_rows();
      foreach($items as $item):
          $rows[] = $item;
      endforeach;     
      $result['rows'] = json_encode($rows);
  
      if (request::is_ajax()) {
          echo json_encode($result);
      }
  }
  </code></pre>
  
  <p>Now I got few questions related to this code. Let me ask them.</p>
  
  <p>1) In thread mentioned above I was told I do not have to use <code>$.parseJSON();</code> function and I can use <code>data.rows[0]name</code> instead. Unfortunately, it does not work. For the is't not a big problem, I can use <code>$.parseJSON();</code> but I'd like to know if there's any bug in my PHP code?</p>
  
  <p>2) I'm trying to learn good practices and techniques and I'm try to not violate MVC rules. I have a MVC related question. Is it possible to use <code>echo()</code> in controllers? You may notice I'm using it to return JSON encoded data.</p>