如何在 cakephp 中检查 Ajax 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2114832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check an Ajax request in cakephp?
提问by udhaya
How to check an Ajax request in cakephp?
如何在 cakephp 中检查 Ajax 请求?
回答by darensipes
Depends on the version of cake.
取决于蛋糕的版本。
1.3.x:
1.3.x:
$this->RequestHandler->isAjax();
2.x || 3.x
2.x || 3.x
$this->request->is('ajax');
回答by pcp
- You need to enable the RequestHandler component
- 您需要启用 RequestHandler 组件
var $components = array('RequestHandler');
var $components = array('RequestHandler');
- Then you check if its an ajax request in your controllers with:
- 然后您检查它是否是您的控制器中的 ajax 请求:
$this->RequestHandler->isAjax()
$this->RequestHandler->isAjax()
You can find more information about the RequestHandler component here
您可以在此处找到有关RequestHandler 组件的更多信息
回答by KonstantinK
this question is an older one but just in case anyone comes across this like me and uses CakePHP 2:
这个问题是一个较旧的问题,但以防万一有人像我一样遇到这个问题并使用 CakePHP 2:
RequestHandler::isAjax()is deprecated, use the Request Object's $this->request->is('ajax');
RequestHandler::isAjax()已弃用,请使用请求对象的 $this->request->is('ajax');
More info here
更多信息在这里
回答by gomflo
Without the use of components you can use something like this:
不使用组件,你可以使用这样的东西:
$this->params['isAjax'];
This will return a bool.
这将返回一个布尔值。
回答by Hyman
this is the way whisch is described is doc.I have been using since i started using cakephp
这就是描述 doc 的方式。自从我开始使用 cakephp 以来,我一直在使用
if($this->RequestHandler->isAjax()){
//
}
回答by Leo
If you simply want to check the function of the php side, try:
如果你只是想检查php端的功能,试试:
$this->log('some debug',LOG_DEBUG);
then check app/tmp/logs/debug.log.
然后检查 app/tmp/logs/debug.log。

