【问题标题】:CakePHP w/ jQuery (Javascript/Ajax) simple link and update - what am I doing wrong?CakePHP w/ jQuery (Javascript/Ajax) 简单链接和更新 - 我做错了什么?
【发布时间】:2011-07-20 04:10:42
【问题描述】:

我只是想用 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.ctp

...
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);
//]]>

【问题讨论】:

  • 我不认为birds_ajax_1_func_1() 被调用。尝试将其直接放在控制器下。 CakePHP 正在寻找birds_controller 的函数,但这个函数是其中一个函数的函数。
  • 你用的是 cake 1.3 还是 1.2?
  • 感谢您的快速回复。实际上,我只是通过询问老师并实现答案综合症解决了这个问题!为了回答这些问题,我使用的是 1.3。主要问题是几个方面。我将发布我的工作解决方案作为我的答案。

标签: php javascript jquery ajax cakephp


【解决方案1】:

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 排序?哦,男孩。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    相关资源
    最近更新 更多