I am overriding a magento controller, before processing, is there a way to know if the request was sent by Ajax or not?
Thanks
Magento uses the class Zend_Controller_Request_Http
for its requests.
You can use
if ($this->getRequest()->isXmlHttpRequest()) {
// is Ajax request
}
to detect Ajax requests this way.
At least
send the HTTP_X_REQUESTED_WITH
header, according to the ZF docs.
Note though, "Ajax requests" means requests sent using XmlHttpRequest (and not using techniques like hidden <iframe>
s, or Flash uploaders, or the like) to me.
Since this is subjective and your perception may differ: Magento itself seems to define "Ajax" in some more extended way than I do. Have a look at Mage_Core_Controller_Request_Http::isAjax()
:
public function isAjax()
{
if ($this->isXmlHttpRequest()) {
return true;
}
if ($this->getParam('ajax') || $this->getParam('isAjax')) {
return true;
}
return false;
}
Depending on your personal perception of "Ajax", this may (or may not) better fit your needs.
Mage::app()->getRequest()->isXmlHttpRequest()
Mar 5, 2015 at 20:36
If Im not mistaken, magento is written using Zend Framework, therefor by using the Request object you can do
if($this->getRequest()->isXmlHttpRequest()){
// ajax
} else {
// not ajax
}
http://framework.zend.com/manual/en/zend.controller.request.html#zend.controller.request.http.ajax
Good luck! :)
Magento internally uses a mix of of both.
Zend Framework's isXmlHttpRequest() checks header.
public function isXmlHttpRequest(){
return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
}
In some cases magento uses isXmlHttpRequest() like in Mage_ImportExport_Adminhtml_ExportController::getFilterAction()
if ($this->getRequest()->isXmlHttpRequest() && $data) {
//code
}
In other cases it checks get parameter like in Mage_Catalog_Product_CompareController::removeAction()
if (!$this->getRequest()->getParam('isAjax', false)) {
$this->_redirectReferer();
}
Request Mage_Core_Controller_Request_Http::isAjax() checks for both
public function isAjax()
{
if ($this->isXmlHttpRequest()) {
return true;
}
if ($this->getParam('ajax') || $this->getParam('isAjax')) {
return true;
}
return false;
}
I would suggest to use Request object isAjax as it checks for Both.
Just use pure PHP and never care:
public function isAjax()
{
return (boolean)((isset($_SERVER['HTTP_X_REQUESTED_WITH'])) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}
$this->getRequest()->getParam('ajax')
- just imagine how many code are executing every time users run different parts of Your app - there is a logic ot determine whether it is ajax or something else, instance of getRequest() (Requests object) and so on... just for this functionality return (boolean)((isset($_SERVER['HTTP_X_REQUESTED_WITH'])) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
Jun 6, 2012 at 13:10
isAjax()
method is slower than using the condition directly, hence performance-wise not the optimum, too. However, no matter whether using the built-in Magento/ZF method, your method, or the condition directly, the performance difference will not be significant at all (some nanoseconds, probably). Re "safer": I fail to see how your method should me more safe.
Jun 6, 2012 at 14:17
You can use this:
if ($this->getRequest()->getParam('ajax')){
//Ajax related code
} else {
//Non ajax
}
ajax
param with their forms (and sometimes an isAjax
param), there also are many Magento controllers (e.g. checkout) that do not do this. And even if so, an existing param ajax
does not tell anything about whether the request was sent using Ajax. Having a simple <input name="ajax" value="1" />
in any form would match your if
condition on submission, too. Even if such form is submitted by POST. Checking for HTTP_X_REQUESTED_WITH
headers tells you s/t more reliable.
Jun 6, 2012 at 10:57
ajax
params. To Magento it seems to be both^^ I updated my answer to reflect this.
Jun 11, 2012 at 10:23