1
我想在CakePHP中進行Ajax調用。第一個調用工作正常,它呈現另一個Ajax提交按鈕的視圖。下面的代碼是在第一視圖:在CakePHP中進行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,但如果LOAD備註點擊
debug($this->request->isAjax());
返回true。這個想法是當點擊加載評論時,評論被加載,然後單擊降級評論(這是對評論的按鈕),我想要進行Ajax調用刪除其評論。非常感謝您的幫助。
我想你忘了添加'echo $ this-> Js-> writeBuffer();'在你的視圖的末尾 – noslone
爲什麼RequestHandler?它將在未來貶值... –
嘗試使用'$ this-> request-> isAjax();'用完整的控制器代碼更新你的問題?它很難理解這個小代碼正在發生什麼.. –