Cakephp $layout will not change

So here is the situation: I have my default.ctp and my ajax.ctp where my ajax.ctp is just the blank $content_for_layout;

When i'm doing an autocomplete ajax call I must change the layout to 'ajax' obviously. So I have this function in my controller

function beforeFilter() {
        if($this->RequestHandler->isAjax()) {
            $this->layout = 'ajax';
        }
    }

However when i look at the results of my autocomplete I get this in my options:

  • DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
  • html xmlns="http://www.w3.org/1999/xhtml">
  • Which basicly is my default layout. So my guess is that the layout didn't change

    Anyone else encountered this problem? Did I forget to add something in my controller ? All the helpers and components are in place.


    There isn't that much that could go wrong in your script. Before filter is good and setting the view template is also. The only thing I can think of is that your ajax request somehow doesn't get recognized.

    The code of RequestHandler::isAjax() is pretty simple:

    function isAjax() {
      return env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest";
    }
    

    So I suggest logging your vars to see if it's recognized as ajax. Also I heard sometimes the caching screws this up. Try $this->disableCache(); before checking if it's ajax.

    Debugging ... isn't it fun

    链接地址: http://www.djcxy.com/p/88456.html

    上一篇: 位置:绝对;

    下一篇: CakePHP $布局不会改变