I want to set $this->layout
to json
in the controller action.
In the json
layout, there will be a line saying $this->Javascript>object();
which will parse through the data given to it by the controller, and output the jSON.
However, creating a new view file for each jSON request, eg. recipe_view
, ingredient_view
isn't necessary, I just need a layout.
Is there a way to bypass the view file altogether and have just the layout, without the notorious Missing View! error?
@pleasedontbelong's solution works. You can also create a layout and view for ajax.
Your layout can be something like this:
<?php echo $content_for_layout;?>
And then you can create an ajax view like this:
<?php echo $this->Js->object($result);?>
And then from your controller...
public function savecontent(){
$this->autoRender = false;
$this->set('result', false);
if(!empty($this->data)){
$data = $this->data;
//Do something with your data
//send results to view
$this->set('result', $myNewData);
}
$this->render(null, 'ajax','/ajax/ajax');
}
hmmm it should be something like this: (not tested)
function action(){
$this->autoLayout = $this->autoRender = false;
// your code
$this->render('/layouts/json');
}
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With