【问题标题】:$this->request->is('ajax') always false$this->request->is('ajax') 总是假的
【发布时间】: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-&gt;request-&gt;is('ajax') 总是错误的 我的 cakephp 版本是 2.3,我已经尝试过 $this-&gt;request-&gt;is('ajax') 等等。 如果有人能指出我错过了什么,我将不胜感激

【问题讨论】:

  • 您可以尝试打开开发者控制台或萤火虫或一些调试工具并复制请求标头吗?另请注意,您的网址 pollsController 很奇怪,请尝试 polls 我怀疑您正在被重定向

标签: javascript php jquery ajax cakephp


【解决方案1】:

在你的代码中,你有

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

尝试这样设置条件:

if ($this->request->is('ajax')) {

}

http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html?highlight=isajax#requesthandlercomponent

RequestHandlerComponent:RequestHandlerComponent 的很多方法都是 只是 CakeRequest 方法的代理。已经采用了以下方法 已弃用并将在未来版本中删除:isSsl() isAjax() isPost() isPut() isFlash() isDelete() getReferer()getClientIp()

【讨论】:

  • 我就是这么做的。结果相同
【解决方案2】:

您是否使用 AJAX 调用发送正确的标头?

{ 'X-Requested-With': 'XMLHttpRequest'}

如果你使用jQuery,你可以使用:

$.ajaxSetup({
    headers: { 'X-Requested-With': 'XMLHttpRequest' }
})

您可以在网络选项卡下的Chrome developer tools 中检查它,您必须在其中选择您的请求。

这里是the documentation for ajaxSetup()

编辑:

你可以放在这里:

<script>
$('#btn').click(function(event) {
    form = $("#PollIndexForm").serialize();
    $.ajaxSetup({
        headers: { 'X-Requested-With': 'XMLHttpRequest' }
    })
    $.ajax({
        type: "POST",
        url: 'pollsController/index';,
        data: form,
        success: function(data){
        }
    });
    event.preventDefault();
    // return false;  //stop the actual form post !important!
});
</script>

【讨论】:

  • jQuery 默认发送,无需设置。
  • 作为一个新手,我从来没有听说过这个,我要放什么文件?我正在使用 cakephp 框架
  • @ceejayoz 不正确,取决于版本、系统、服务器等。例如,我必须这样做...
  • @user3340752 只需将其放在您的 AJAX 调用之前的任何位置
  • 这样做后仍然没有修复
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多