简体   繁体   English

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

[英]How can I display ajax results in a view in CakePHP without including the default template in the ajax response?

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. 我正在使用CakePHP中的AJAX进行一些实验,它似乎工作,除了返回的视图包含默认模板。 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. Ajax是一个包含的空白布局,以防止添加额外的标记,正是您想要的。

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

Try using RequestHandler component. 尝试使用RequestHandler组件。 This will be handled automatically for you. 这将自动为您处理。 Then, you can do something like this in your AppController::beforeFilter() 然后,您可以在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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM