【问题标题】:Ajax is not working in Cakephp 2.2.3Ajax 在 Cakephp 2.2.3 中不起作用
【发布时间】:2013-01-23 13:15:09
【问题描述】:

我想在 ContactUs 表单上实现 Ajax,这是我的代码。 控制器:

class ContactsController extends AppController {

public $layout = 'default';
public $helpers = array('Html', 'Form', 'Paginator','Js');
public $components = array('RequestHandler');

public function index(){
    if(!empty($this->data)){
        if($this->Contact->save($this->data)){
            if($this->RequestHandler->isAjax()){
                $this->render('success','ajax');
            } else {
            $this->Session->setFlash('Message sent');
            $this->redirect(array('action'=>'index'));
            }
        }
    }
}
}

查看文件

<?php echo $this->Html->script('jquery', FALSE); ?>
    <div id="success"></div>
    <h2>Contact Us</h2>

    <?php
            echo $this->Form->create();
            echo $this->Form->input('name',array('id'=>'name'));
            echo $this->Form->input('email',array('id'=>'email'));
            echo $this->Form->input('message',array('id'=>'message'));
            echo $this->Js->submit('Send',array(
                'before'=>$this->Js->get('#sending')->effect('fadeIn'),
                'success'=>$this->Js->get('#sending')->effect('fadeout'),
                'update'=>'#success'
            ));
            echo $this->Form->end();                
            ?>                
<div id="sending" style=" display: none;background-color: #90ee90;">Sending...</div>

在 view/layouts/ajax.ctp 文件中包含:

<?php //echo $this->fetch('content'); ?>
<?php echo $content_for_layout; ?>

这两个我都试过了。并且 default.ctp 布局包含在 head 部分:

echo $scripts_for_layout;
echo $this->Js->writeBuffer(array('cache'=>TRUE)); 

在源文件中包含 jquery,但 PHPStorm 在 jquery 文件中显示错误,并且 firebug 显示

希望一切都清楚,非常感谢任何帮助。

【问题讨论】:

  • @Oldskool 如果你在这里,请帮助我。

标签: ajax cakephp-2.2


【解决方案1】:

你应该禁用 before 和 success 回调上的缓冲区。

echo $this->Js->submit('Send',array(
    'before'=>$this->Js->get('#sending')->effect('fadeIn', array('buffer' => false)),
    'success'=>$this->Js->get('#sending')->effect('fadeOut', array('buffer' => false)),
    'update'=>'#success'
));

编辑: 错字也可能导致此问题:'fadeout' 而不是 'fadeOut'。

【讨论】:

    【解决方案2】:

    我遇到了完全相同的问题。我看到你正在使用 Andrew Perkins 的 Cakephp 教程(我使用的是完全相同的,所以我的代码是相同的)http://www.youtube.com/watch?v=dQ71psonQx0

    我相信问题在于他基于 Cakephp 1.3 制作了这个视频教程。语法在 2.0 中发生了很大变化。我还没有弄清楚如何让它在 2.2.3 中工作,但我认为这就是原因。

    【讨论】:

    • 不,我没有遵循任何教程,我只是在使用 twitter 引导程序。
    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多