CakePHP w / jQuery(Javascript / Ajax)简单链接和更新 - 我做错了什么?

时间:2011-07-20 04:10:43

标签: php javascript jquery ajax cakephp

我只是想尝试使用jQuery打印出一个动作的内容。但我无法通过回显的内容更新我的div。我做错了什么?

birds_controller.php

...
    var $helpers = array('Js', 'Html', 'Ajax');
    var $components = array('RequestHandler');

function birds_ajax_1() {
    function birds_ajax_1_func_1() {
        $this->autoRender = false;
        echo "Text from Ajax clickaroo.";
    }
}
...

birds_ajax_1.ctp

<?php
    echo $ajax->link('Ajax Link', array('controller' => 'birds',
    'action' => 'birds_ajax_1_func_1'), 
         array('update' => 'ajax_div', 'complete' => 'alert("Ok.")'));
?>

<div id="ajax_div"></div> <!-- echoed text is not showing here? -->

default.thtml中

...
echo $this->Html->script('jquery'); // Yes. My jQuery is being pulled in OK.
...

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

这是我的来源:

...
        <a href="/php/cake_app_birds_v1.3/birds/birds_ajax_1_func_1"     id="link2019467549" onclick=" event.returnValue = false; return false;">link1</a><script     type="text/javascript">

//<![CDATA[
Event.observe('link2019467549', 'click', function(event) { new Ajax.Updater('ajax_div','/php/cake_app_birds_v1.3/birds/birds_ajax_1_func_1',     {asynchronous:true, evalScripts:true, onComplete:function(request, json) {alert("Ok.")},     requestHeaders:['X-Update', 'ajax_div']}) }, false);
//]]>

1 个答案:

答案 0 :(得分:2)

<强> birds_controller.php

var $helpers = array('Js' => array('Jquery'), 'Html', 'Ajax');
var $components = array('RequestHandler');

function birds_ajax_1() {
    if ($this->RequestHandler->isAjax()) {
    $this->render('/elements/birds_ajax_1'); // this birds_ajax_1.ctp contains the output into the #ajax_div.
    }
}

<强> birds_ajax_1.ctp

<?php
// Note I am not using the $ajax->link(...
echo $this->Js->link('Ajax Link', array('controller' => 'birds',
'action'     => 'birds_ajax_1'), array('update' => '#ajax_div'));

?>

<div id="ajax_div"></div>

default.ctp中没有任何变化。

希望这对可能正在经历我刚刚经历的困惑的其他人有所帮助!很高兴我找到了光!现在进入一些jQuery排序?哦,小伙子。