# I cannot get json responses from cakephp3 controller

**URL:** <https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683>\
**Category:** Need Help\
**Created:** [July 4, 2016, 7:24pm UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683 "2016-07-04T19:24:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardoassaad](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@ricardoassaad](https://discourse.cakephp.org/u/ricardoassaad)\
**Post date:** [July 4, 2016, 7:24pm UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683/1 "2016-07-04T19:24:52Z")

</div>

Hi all, i want to ask for your advice on what i am doing wrong  
what i am trying to do is making an ajax call and getting any type of response from the controller. the response will be displayed in the page that initiated the ajax call but the controller never sends data back. what is weird is that the debug\_kit detects the response and displays it so i get the \_serialize and the result variable but not the javascript function

this is the javascript function:  
`<script type = "text/javascript" language = "javascript"> $(document).ready(function(){ $("button").click(function(){ $.ajax({ type : "GET", url : "<?php echo $this->Url->build(['controller' => 'users', 'action' => 'getByCategories', '_ext' => 'json']); ?>", contentType : false, processData : false, dataType : 'json', success: function(response){ console.log(JSON.parse(response)); } }); }); }); </script>`

this is the action in the controller  
`public function getByCategories() { $id = 33; $this->autoRender = false; $cat = $this->Users->Categories->get($id); if ($this->Users->Categories->childCount($cat) > 0) { $result = $this->Users->Categories->find('all', ['conditions' => ['parent_id' => $id]]); $this->set(compact('result')); $this->set('_serialize', ['result']); } else { $result['Machines'] = $this->Users->Machines->find('all', ['conditions' => ['category_id' => $id]]); $result['Medias'] = $this->Users->Medias->find('all', ['conditions' => ['category_id' => $id]]); $this->set(compact('result')); $this->set('_serialize', ['result']); } }`

if i try to access the link of this action i get null

i added the routes for the json extension  
i loaded the request handler component.

the debug kit receives the response but not the javascript function

thanking you in advance

---

<div class="post-metadata">

**Author:** ![ali](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ali/32/123_2.png) [@ali](https://discourse.cakephp.org/u/ali)\
**Post date:** [July 5, 2016, 2:04am UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683/2 "2016-07-05T02:04:48Z")

</div>

I think you’re not rendering the view at all, so it’s normal that nothing is displayed.

You can try to add die(json\_encode($result)) instead of $this-\>set(compact(‘result’));  
That should give you the expected results.

---

<div class="post-metadata">

**Author:** ![ricardoassaad](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@ricardoassaad](https://discourse.cakephp.org/u/ricardoassaad)\
**Post date:** [July 5, 2016, 11:31pm UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683/3 "2016-07-05T23:31:34Z")

</div>

Thank you ali this did the trick but i want to ask you if i want to use the \_serialize how should i do it. Because i read somewhere that i need to disable the view so i can get the result in the same page that is making the request

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [July 6, 2016, 10:23am UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683/4 "2016-07-06T10:23:34Z")

</div>

Did you added json handling at your `/config/routes.php` file?

`Router::extensions(['json']);`

---

<div class="post-metadata">

**Author:** ![ttorres](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ttorres/32/263_2.png) [@ttorres](https://discourse.cakephp.org/u/ttorres)\
**Post date:** [July 13, 2016, 3:35am UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683/5 "2016-07-13T03:35:31Z")

</div>

You can test RESTful operations using the [fiddler](http://www.telerik.com/fiddler) tool to determine if your creating any json data. This will allow you to concentrate on only the data being retrieved to better isolate the problem.

---

<div class="post-metadata">

**Author:** ![ricardoassaad](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@ricardoassaad](https://discourse.cakephp.org/u/ricardoassaad)\
**Post date:** [July 13, 2016, 7:09am UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683/6 "2016-07-13T07:09:08Z")

</div>

Thanks but yes i added it and btw ali’s response did the trick  
thanks again

---

<div class="post-metadata">

**Author:** ![ricardoassaad](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@ricardoassaad](https://discourse.cakephp.org/u/ricardoassaad)\
**Post date:** [July 13, 2016, 8:07am UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683/7 "2016-07-13T08:07:48Z")

</div>

the problem was that i was not getting any response back so the problem was in the cakePHP action

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [July 28, 2016, 12:56pm UTC](https://discourse.cakephp.org/t/i-cannot-get-json-responses-from-cakephp3-controller/683/8 "2016-07-28T12:56:18Z")

</div>

thanks for the update
