cakehp-2.0最简单的ajax链接与jshelper


cakephp-2.0 simplest ajax link with jshelper

我想创建Cakephp2.0中最基本的ajax链接。

索引.ctp中,我有

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

TechnologiesController.php中我有

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

视图中。ctp我有

<?php echo $msg;?>

它没有在successdiv中设置视图,而是导航到http://local.cake.com/technologies/view页面以显示消息。

非常感谢您的帮助!

默认情况下,脚本是缓存的,您必须显式打印出缓存。要在每一页的末尾执行此操作,请在结束标记前包含以下行:

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

我在Layouts Folder 中的default.ctp的末尾使用它

请确保在控制器中设置$components=数组("RequestHandler")

所以,总的来说,代码看起来是这样的——它对我有效(CakePHP 2.2.4):

index.ctp:

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

非常感谢,这帮助我了解了2.0及以上版本中的工作原理:)

尝试$this->autoRender = FALSE;

我想你可能不得不放这个:

echo $this->Js->writeBuffer();

例如$this->Js->link调用的正下方。

希望能有所帮助!

我刚刚放了这个:

echo $this->Js->writeBuffer();

它对我有效,希望我会为你