如何在CakePHP中的视图中显示ajax结果,而不在ajax响应中包含默认模板?

I'm doing some experimenting with AJAX in CakePHP and it seems to work except that the view that gets returned includes the default template. How can I get rid of that (or even just specify a different empty template for a view)?

function ajaxFunction() {
    //do stuff
    $this->layout= 'ajax';
}

Ajax is an included blank layout to prevent extra markup being added, exactly what you want.

http://book.cakephp.org/view/96/Layouts

Try using RequestHandler component. This will be handled automatically for you. Then, you can do something like this in your AppController::beforeFilter()

if($this->RequestHandler->isAjax()) {
    Configure::write('debug',0);
}

You will also need to turn off debug output otherwise cake will squirt out all the debug info you usually see at the bottom of the page:

function ajaxFunction() {
    //do stuff
    Configure::write('debug', 0);
    $this->layout= 'ajax';
}