4

我想创建 Cakephp 2.0 中最基本的 ajax 链接。

index.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'); 
}

view.ctp我有

<?php echo $msg;?>

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

非常感谢任何帮助!

4

6 回答 6

6

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

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

我在布局文件夹中的 default.ctp 末尾使用它

于 2012-12-20T08:37:30.520 回答
1

所以,总的代码看起来像这样 - 它适用于我(CakePHP 2.2.4):

索引.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 及更高版本的工作原理:)

于 2013-01-06T10:18:36.167 回答
1

请务必在控制器中设置 $components = array('RequestHandler')

于 2012-03-30T12:32:26.707 回答
0

我想你可能不得不这样说:

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

某处 - 例如在$this->Js->link通话下方。

希望有帮助!

于 2012-04-10T21:34:48.097 回答
0

尝试$this->autoRender = FALSE;

于 2012-03-30T07:33:37.520 回答
0

我只是把这个:

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

它对我有用,希望我对你有用

于 2013-08-01T10:29:26.097 回答