Access-Control-Allow-Origin in symfony
<p>I use a JSON response in a Symfony controller. When my result is false, Access-Control-Allow-Origin is OK, but when result is true, the Access-Control-Allow-Origin doesn't permit access.</p>

<pre><code>$em = $this-&gt;getDoctrine()-&gt;getManager();

$submited = Request::createFromGlobals();
$id = $submited-&gt;request-&gt;get('id');

$user = $this-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser();
$link = $em-&gt;getRepository('AdminBundle:VisitGoogle')-&gt;findOneBy(array('id' =&gt; $id, 'status' =&gt; 0));

if ($link) {
    $link-&gt;setStatus(1);
    $em-&gt;persist($link);
    $em-&gt;flush();

    $user-&gt;setEarn($user-&gt;getEarn() + $link-&gt;getCredit());
    $em-&gt;persist($user);
    $em-&gt;flush();

    $response = array("success" =&gt; true, 'message' =&gt; 'google is ok');
    $response = new JsonResponse($response);
    $response-&gt;headers-&gt;set('Access-Control-Allow-Origin', '*');

    return $response;
} else {
    $response = array("success" =&gt; false, 'message' =&gt; $this-&gt;get('translator')-&gt;trans('message.linkiswrong'));
    $response = new JsonResponse($response);
    $response-&gt;headers-&gt;set('Access-Control-Allow-Origin', '*');

    return $response;
}
</code></pre>

<p>Why is this happening? How can I fix it?</p>