title: CakePHP 3 - Can't return proper json when debug mode = true
body: <p>I'm new to stackoverflow, and I've just started to play around with CakePHP 3.</p>
  
  <p>I've run into a weird problem:</p>
  
  <p>I'm sending an ajax-request (form submit) to the controller, and I expect to get a proper json-response back. It works fine when I set debug mode to false in config/app.php, but when it's set to true, I get an error-message in the browsers console, and the responsetext seem to be html. I'm calling the action with the .json extension in the url.</p>
  
  <p>I've linked screenshot of the console where the first response is with debug mode set to false, and the second set to true:</p>
  
  <p><a href="https://i.stack.imgur.com/cOwkO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cOwkO.png" alt="enter image description here"></a></p>
  
  <p>I have enabled the extensions in config/routes.php:</p>
  
  <pre><code>Router::scope('/', function (RouteBuilder $routes) {
      $routes-&gt;extensions(['json', 'xml']);
  (...)
  </code></pre>
  
  <p>Here's the controller-code:</p>
  
  <pre><code>public function getUserStats() {
      $this-&gt;log($this-&gt;request-&gt;data, 'debug');
  
      if (($this-&gt;request-&gt;is('post'))) {
          $this-&gt;log('getCategories(): Post-request is received.', 'info');
  
          $usersTable = TableRegistry::get('Users');
          $q = $usersTable-&gt;find('statsByUsers', $this-&gt;request-&gt;data);
          $users = $q-&gt;all();
  
          // Calculating total amount per user.               
          foreach ($users as $u) {
              foreach ($u-&gt;purchases as $p) {
                  $u-&gt;total += $p-&gt;total;
              }
          }
  
          $this-&gt;log($users, 'debug');
  
          $this-&gt;set('users', $users);
          $this-&gt;set('_serialize', ['users']);
      }
  }
  </code></pre>
  
  <p>Here's the model code:</p>
  
  <pre><code> public function findStatsByUsers(Query $query, array $options) {
      debug($options);
      $options['dates'] = $this-&gt;getConvertedDates($options);
      $query
          -&gt;contain([
              'Purchases' =&gt; function($q) use($options) {
                  return $q
                      -&gt;select(['id', 'total' =&gt; 'amount * count', 'purchase_date', 'user_id'])
                      -&gt;where(['purchase_date BETWEEN :fromDate AND :toDate',])
                      -&gt;bind(':fromDate', $options['dates']['fromDate'], 'datetime') // Binds the dates to the variables in where-conditions
                      -&gt;bind(':toDate', $options['dates']['toDate'], 'datetime');
              }
                  ])
              -&gt;where([
                  'Users.id IN ' =&gt; $options['users'],
                  'Users.active' =&gt; true
          ]);
  
      return $query;
  }
  </code></pre>
  
  <p>I hope I've given you enough information so that you can help me solve this.</p>
  
  <p>CakePHP version: 3.3.2</p>