【问题标题】:Cake php ajax layout cacheCake php ajax 布局缓存
【发布时间】:2014-05-29 23:35:22
【问题描述】:

在任何 ajax 请求之后,所有后续的非 ajax 请求都以 ajax 布局而不是默认布局返回。

观察:

  • 仅在生产环境中出现。
  • 配置::write('Cache.disable', true); // 没有任何效果!
  • 蛋糕版本 2.4.4
  • 在 20 ~ 30 秒后呈现布局(默认)。
  • config/core.php 相同。

我不知道为什么,我为此浪费了 8 个小时,有什么提示吗?

控制器(但任何控制器上的任何 ajax 都会导致问题:

<?php

App::uses('AppController', 'Controller');

class NewslettersController extends AppController
{

public $paginate = array(
    'limit' => 20,
    'paramType' => 'querystring',
    'order' => array(
        'Newsletter.created' => 'DESC'
    )
);

public function beforeFilter()
{
    parent::beforeFilter();
    $this->Auth->allow(array('add','push'));
}

public function admin_index()
{
    $this->set('dataGrid', $this->paginate('Newsletter'));
}

public function admin_view($id = null)
{
    $this->Newsletter->id = $id;
    $result = $this->Newsletter->read();
    if (!$result) {
        $this->setFlashMessage('Cadastro não encontrado', 'error', array('action' => 'index'));
        return false;
    }

    $this->set('dataGrid', $result);
}

public function admin_delete($id = null)
{
    $this->Newsletter->id = $id;

    if (!$this->Newsletter->exists()) {
        $this->Session->setFlash('O item solicitado não foi encontrado!', 'alert_error');
        $this->setFlashMessage('O item solicitado não foi encontrado!', 'error', array('action' => 'index'));
    }

    try {
        if ($this->Newsletter->delete($id, false)) {
            $this->setFlashMessage('Item excluído com sucesso!', 'success', array('action' => 'index'));
        }
    } catch (Exception $e) {
        $this->setFlashMessage('Não foi possivel excluir este item pois existem itens atrelados a ele', 'error', array('action' => 'index'));
    }
}

public function admin_search()
{
    $this->autoRender = false;
    $conditions = null;
    if (isset($this->request->query['search']) && !empty($this->request->query['search'])) {
        $conditions[] = array(
            'OR' => array(
                'Newsletter.name LIKE' => '%' . $this->request->query['search'] . '%',
                'Newsletter.email LIKE' => '%' . $this->request->query['search'] . '%',
            )
        );
        $this->paginate['conditions'] = $conditions;
        $this->set('dataGrid', $this->paginate());
        $this->render('admin_index');
    }
}

//######################
//# FRONTEND           #
//######################

public function push()
{

    if($this->Newsletter->save($this->request->data))
    {
        $response = array("result"=>true,"id"=>$this->Newsletter->id);
    }
    else
    {
        $response = array("result"=>false,"errors"=>$this->Newsletter->validationErrors);
    }

    return new CakeResponse(array("body" => json_encode($response),"type" => "json"));

}

}

?>

【问题讨论】:

  • 让我们看看你的控制器............
  • 我用控制器代码更改帖子!

标签: php ajax cakephp caching layout


【解决方案1】:

你必须在方法中区分ajax的布局,请在上面的代码中进行以下更改:

...
class NewslettersController extends AppController
{

   public layout = 'default';
...

   public function push() // if this is ajax function
   {
      $this->layout = 'ajax'; //put this for ajax functions only
      ....
   }

...
}

希望对你有帮助!

【讨论】:

  • 感谢我正在解决问题,不是蛋糕 php 错误是 HTTP_X_REQUESTED_WITH 标头缓存问题....
猜你喜欢
  • 2011-08-10
  • 2013-07-11
  • 1970-01-01
  • 2012-03-22
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多