【发布时间】:2014-02-22 13:59:24
【问题描述】:
我的视图中有代码,但通过 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很奇怪,请尝试 polls 我怀疑您正在被重定向
标签: javascript php jquery ajax cakephp