Cakephp3: How can I return json data? : <p>I am having a ajax post call to a cakePhp Controller:</p>

<pre><code>$.ajax({
                type: "POST",
                url: 'locations/add',
                data: {
                  abbreviation: $(jqInputs[0]).val(),
                  description: $(jqInputs[1]).val()
                },
                success: function (response) {
                    if(response.status === "success") {
                        // do something with response.message or whatever other data on success
                        console.log('success');
                    } else if(response.status === "error") {
                        // do something with response.message or whatever other data on error
                        console.log('error');
                    }
                }
            });
</code></pre>

<p>When I try this I get the following error message:</p>

<blockquote>
  <p>Controller actions can only return Cake\Network\Response or null. </p>
</blockquote>

<p>Within the AppController I have this</p>

<pre><code>$this-&gt;loadComponent('RequestHandler');
</code></pre>

<p>enabled.</p>

<p>the Controller function looks  like this:</p>

<pre><code>public function add()
{
    $this-&gt;autoRender = false; // avoid to render view

    $location = $this-&gt;Locations-&gt;newEntity();
    if ($this-&gt;request-&gt;is('post')) {
        $location = $this-&gt;Locations-&gt;patchEntity($location, $this-&gt;request-&gt;data);
        if ($this-&gt;Locations-&gt;save($location)) {
            //$this-&gt;Flash-&gt;success(__('The location has been saved.'));
            //return $this-&gt;redirect(['action' =&gt; 'index']);
            return json_encode(array('result' =&gt; 'success'));
        } else {
            //$this-&gt;Flash-&gt;error(__('The location could not be saved. Please, try again.'));
            return json_encode(array('result' =&gt; 'error'));
        }
    }
    $this-&gt;set(compact('location'));
    $this-&gt;set('_serialize', ['location']);
}
</code></pre>

<p>What do I miss here? Is there any additional settings needed?</p>
