在CakePHP中进行Ajax调用

在CakePHP中进行Ajax调用,ajax,cakephp,Ajax,Cakephp,我正在尝试用CakePHP进行Ajax调用。第一个调用工作正常,它使用另一个Ajax提交按钮呈现视图。以下代码在第一视图中: echo $this->Js->submit('LOAD COMMENTS',array( 'url'=>array( 'controller'=>'Nominees', 'action'=>'load_comments'

我正在尝试用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();
在我的提名中

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,但如果单击了加载注释

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

返回true。这个想法是当点击Load Comments(加载注释)时,加载注释,然后点击Demode Comments(降级注释)时,这是一个针对注释的按钮,我想通过Ajax调用删除注释。非常感谢您的帮助。

我终于找出了我在参考中的错误所在

在我的提交按钮上,我使用回调函数在数组中添加了'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。这使我的代码按预期工作:-

我想你忘了添加echo$this->Js->writeBuffer;在viewWhy RequestHandler的末尾?它将在未来贬值…尝试使用$this->request->isAjax;并用完整的控制器代码更新您的问题??很难理解这个小代码是怎么回事。@FazalRasel我添加了完整的控制器操作代码,并删除了RequestHandler。希望它现在更清晰。我有echo$this->Js->writeBuffer;在我的默认布局中。你应该回显$this->Js->writeBuffer;在所有Ajax渲染视图中也是如此。