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.

Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58
Chris De Rouck
  • 127
  • 2
  • 5
  • @BenjaminAllison: This question was migrated from WebApps.SE almost 2 weeks ago. My comment is now irrelevant and I'm removing it. – dnbrv Apr 04 '12 at 11:38
  • 1
    also, for reference, I think it should be `$this->set('_serialize', array('bookings'));` to avoid multiple roots (see the updated doc on this one) – mark Apr 27 '12 at 08:03
  • 1
    I'm not sure but I think something is missing -> the attribute $viewClass in the Controller. http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-1.html#jsonview – raultm Jul 09 '12 at 12:14

2 Answers2

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.

Ryan White
  • 1,927
  • 2
  • 19
  • 32
1

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

marknatividad
  • 630
  • 8
  • 15