title: Enabling CORS in CakePHP app
body: <p>I am trying to enable CORS for an API built in CakePHP so that all requests are accessible with the following in the AppController:</p>
  
  <pre><code>public function beforeFilter()
  {
      header("Access-Control-Allow-Origin: *");
  }
  </code></pre>
  
  <p>Is this in the wrong place? As requests are still being blocked.</p>
  
  <p><strong>Update: It seems this does in fact work BUT because I am doing something like:</strong></p>
  
  <pre><code>header('Content-Type: application/json');
  echo json_encode(array('message'=&gt;'Hello world!'));
  </code></pre>
  
  <p><strong>In some of my methods it's acting as though it's overriding the header set the AppController so it's not appearing in the response for the JSON calls.
  Any ideas?</strong></p>
  
  <p><strong>Update 2: Returning JSON like below, fixes the problem:</strong></p>
  
  <pre><code>$this-&gt;response-&gt;type('json');
  $this-&gt;response-&gt;body(json_encode(array('message'=&gt;'Hello world!')));
  </code></pre>
  
  <p><strong>So apparently using <code>header()</code> in Cake breaks previous headers?</strong></p>