Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp JsonView

I'm using the new CakePHP 2.1 and would like to use the JsonView to make my controller respond on an ajax request created by JQuery on client side. However this should be done automatically with the JsonView according to the documentation.

http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

I added this line in my routes.php file

Router::parseExtensions('json');

And in my controller i have

$this->RequestHandler->setContent('json', 'application/json' ); 
$bookings = $this->Bookings->find('all');

$this->set('bookings', $bookings);  
$this->set('_serialize', 'bookings');

Then the view should be obsolete, but when i call this, he still serves a page which is pointing to a missing view.

like image 348
Chris De Rouck Avatar asked Mar 24 '12 13:03

Chris De Rouck


2 Answers

Are you making the request with the application/json header?

Try making a request to /controller/method.json This should force the view. If that works then your headers are probably not being set right.

like image 176
Ryan White Avatar answered Nov 08 '22 15:11

Ryan White


Have you created the view file inside /views/controller_name/json/action.ctp?

like image 34
marknatividad Avatar answered Nov 08 '22 16:11

marknatividad