drq9991 2011-07-20 04:10
浏览 57
已采纳

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

I am simply trying to use jQuery to print out the contents of an action. But I cannot get my div to update with the echoed content. What am I doing wrong?

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.ctp

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

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

This is what is being dumped to my source:

...
        <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条回答 默认 最新

  • dongli2000 2011-07-20 04:36
    关注

    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>
    

    Nothing changed in default.ctp.

    Hope this is helpful to someone else who might be going through the confusion I just went through! Glad I found the light! Now onto some jQuery sorting ? Oh boy.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料