我在我的视图中有代码,但通过ajax发送给我的控制器动作(如add.ctp的最后部分所示)
//add.ctp
<?php
echo $this->Form->create('Poll',array('action' => 'index'));
echo $this->Form->input('one', array('id'=>'name'));
echo $this->Form->input('two', array('id'=>'email'));
echo $this->Form->input('three', array('id'=>'message'));
echo $this->Form->input('four', array('id'=>'four'));
echo $this->Js->submit('Send', array('id' => 'btn'), 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: lightgreen;">Sending...</div>
<script>
$('#btn').click(function(event) {
form = $("#PollIndexForm").serialize();
// console.log(form);
$.ajax({
type: "POST",
url: 'pollsController/index';,
data: form,
success: function(data){
//
}
});
event.preventDefault();
// return false; //stop the actual form post !important!
});
</script>
在得到我的控制器
,我做了一个isAjax
要求测试,如果失败
public $components = array('RequestHandler');
public function index(){
$this->autoRender = false;
if($this->RequestHandler->isAjax()){
echo debug('Ajax call');
}
if(!empty($this->data)){
echo debug('not empty');
}
}
每次都遇到我试图运行这个和$this->request->is('ajax')
永远是假的时候“不空” 我的CakePHP的版本是2.3和我已经尝试$this->request->is('ajax')
和所有。 什么,我错过了
你能尝试打开开发者控制台或萤火虫或一些调试工具,并复制请求标头?另外请注意您的网址'pollsController'是很奇怪的,尽量只** **投票我怀疑你将被重定向 – lp1051