在CakePHP中进行Ajax调用

时间:2014-04-22 14:39:33

标签: ajax cakephp

我正在尝试在CakePHP中进行Ajax调用。第一个调用工作正常,它使用另一个Ajax提交按钮呈现视图。以下代码在第一个视图中:

echo $this->Js->submit('LOAD COMMENTS',array(
                'url'=>array(
                    'controller'=>'Nominees',
                    'action'=>'load_comments'
                    ),
                'before'=>$this->Js->get('#sending')->effect('fadeIn'),
                'success'=>$this->Js->get('#sending')->effect('fadeOut'),
                'update'=>'#comments'
                ));
echo $this->Form->end();

在Nominees控制器中我有

public $components = array('RequestHandler');
/*Load comments for the nominee using ajax*/
public function load_comments(){

    if(!empty($this->request->data)){

        /* Demoting Irrelevant comments.
Only triggered if the demote comment button is clicked.*/
        if(isset($this->request->data['Nominee']['comment_nominee_id'])){
            $comment_id = $this->request->data['Nominee']['comment_nominee_id'];
            $this->Nominee->updateAll(
            array('Nominee.status' =>0),
            array('Nominee.id'=>$comment_id));
        }

        $userNumber = $this->request->data['Nominee']['number'];
        $award_id = $this->request->data['Nominee']['award'];
        $dt = date('Y');
        $nominationInfor = $this->Nominee->find('all',array(
                            'conditions' => array('Nominee.reg_number'=>$studNumber,'Nominee.year'=>$dt,'Nominee.award_id'=>$award_id,'Nominee.status'=>1),
                            'recursive' => -1
                            ));
        if(empty($nominationInfor)){
            $this->Session->setFlash(__('No Nomination Information Available.'));
            $this->redirect(array('action' => 'choose_category'));
        }
        $this->set(compact('nominationInfor','userNumber','award_id'));

        if($this->request->isAjax()){

            $this->render('load_comments','ajax');
        }

    }
}

Load_comments,即被调用的视图,有一个Ajax按钮,其视图代码如下所示:

echo $this->Js->submit('Demote Comment',array(

                'url'=>array(
                    'controller'=>'AwardNominees',
                    'action'=>'load_comments'
                    ),
                'before'=>$this->Js->get('#demot')->effect('fadeIn'),
                'success'=>$this->Js->get('#demot')->effect('fadeOut'),
                'update'=>'#comments'
                ));

            echo $this->Form->end();

单击“降级注释”按钮时,

debug($this->request->isAjax());

返回false,但如果单击LOAD COMMENTS

debug($this->request->isAjax());

返回true。这个想法是当点击加载注释时,加载注释然后单击Demote Comment(这是注释的按钮)我想做一个Ajax调用删除其注释。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我终于弄明白我在参考this

时出错了

在我的提交按钮上,我使用我的回调函数在数组中添加了'buffer'=> false,因此最终提交如下所示

                echo $this->Js->submit('Demote Comment',array(
                    'url'=>array(
                    'controller'=>'AwardNominees',
                    'action'=>'load_comments'
                    ),
                'before'=>$this->Js->get('#demot')->effect('fadeIn'),
                'success'=>$this->Js->get('#demot')->effect('fadeOut'),
                'update'=>'#comments',
                'buffer' => false
                ));

我将不同的id添加到我创建的表单中。这使我的代码按预期工作。 : - )