Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp-2.0 simplest ajax link with jshelper

I want to create the most basic ajax link there is in Cakephp 2.0.

In index.ctp i have

 <?php
echo $this->Js->link('myLink', array('controller'=>'technologies', 'action'=>'view'), array('update'=>'#success'));
?>
 <div id="success"></div>

in TechnologiesController.php i have

public function view(){
    $this->set('msg', 'message');
    $this->render('view', 'ajax'); 
}

and in view.ctp i have

<?php echo $msg;?>

Instead of setting the view in the success div, it navigates to the http://local.cake.com/technologies/view page to display the message.

Any help much appreciated!

like image 542
glasspill Avatar asked Feb 20 '23 23:02

glasspill


2 Answers

By default scripts are cached, and you must explicitly print out the cache. To do this at the end of each page, include this line just before the ending tag:

echo $this->Js->writeBuffer(); // Write cached scripts

I am using this at the end of my default.ctp in the Layouts Folder

like image 174
Markus Avatar answered Feb 28 '23 21:02

Markus


Be sure to set $components = array('RequestHandler') in your controller

like image 21
Wil Avatar answered Feb 28 '23 21:02

Wil