Cakephp JsonView
Asked Answered
R

2

7

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.

Rotatory answered 24/3, 2012 at 13:34 Comment(3)
@BenjaminAllison: This question was migrated from WebApps.SE almost 2 weeks ago. My comment is now irrelevant and I'm removing it.Haug
also, for reference, I think it should be $this->set('_serialize', array('bookings')); to avoid multiple roots (see the updated doc on this one)Balkin
I'm not sure but I think something is missing -> the attribute $viewClass in the Controller. book.cakephp.org/2.0/en/appendices/…Alphonsa
O
5

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.

Oza answered 26/4, 2012 at 18:23 Comment(0)
G
1

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

Gash answered 11/4, 2012 at 5:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.