title: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
body: <p>I have the following AJAX call:</p>
  
  <pre><code>$('#FonykerUsernameRegister').blur(function(){
              if($(this).val().length &gt; 2) {
                  alert($(this).val());
                  $.ajax({
                      url: '&lt;?php echo $html-&gt;url('/fonykers/validate_username',true); ?&gt;' + '/' + $(this).val(),
                      dataType: 'json',
                      type: 'POST',
                      success: function(response) {
                          if(!response.ok) {
                              $(this).addClass('error');
                              error.html(response.msg);
                              error.fadeIn();
                          } else {
                              if($(this).is('.error')) {
                                  alert('im in');
                                  $(this).removeClass('error');
                              }
                              $(this).addClass('ok');
                          }
                      },
                      error:function (xhr, ajaxOptions, thrownError){
                          alert('You fail');
                          alert(xhr.statusText);
                          alert(thrownError);
                      } 
                  });        
              } else {
                 error.html('Username must have at least 3 characters');
                 error.fadeIn();
                 $(this).addClass('error');
              }
          });
  </code></pre>
  
  <p>And this is the method it's calling:</p>
  
  <pre><code>function validate_username($username) {
          $this-&gt;layout = 'ajax';
          $response = array();
          $fonyker = $this-&gt;Fonyker-&gt;find('first', array(
              'conditions' =&gt; array('Fonyker.username' =&gt;  $username)
          ));
  
          if(!strlen($username) &gt; 2) {
              $response = json_encode(array('ok' =&gt; false, 'msg' =&gt; 'Username must have at least 3 characters'));
          } else if(!preg_match('"^[a-zA-Z0-9]+$"', $username)) {
              $response = json_encode(array('ok' =&gt; false, 'msg' =&gt; 'Username must be alphanumeric'));
          } else if ($fonyker) {
              $response = json_encode(array('ok' =&gt; false, 'msg' =&gt; 'Username is already in use'));
          } else {
              $response = json_encode(array('ok' =&gt; true, 'msg' =&gt; ''));
          }
  
          echo $response;
      }
  </code></pre>
  
  <p>In the AJAX call it always goes to the error part and outputs that error, and I don't see any whitespaces in my PHP code. Any thoughts?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>What was happening is that CakePHP was echoing the entire view in the response, but not really the view but the error screen.</p>
  
  <p>If you need AJAX methods in your controller that do not require a view you must set the <code>$this-&gt;autoRender</code> property on the controller method to <code>false</code> before doing anything.</p>