I am overriding a magento controller, before processing, is there a way to know if the request was sent by Ajax or not?
Thanks
$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });
Previously, ASP.NET MVC applications could easily check if a request was being made via AJAX, through the aptly named IsAjaxRequest() method which was an available method on the Request object, as shown below: public ActionResult YourActionName() { // Check if the request is an AJAX call.
The X-Requested-With header returns a string that indicates whether it's an Ajax request or not. An Ajax request will have this header set to XMLHttpRequest.
Previously, ASP.NET MVC applications could easily check if a request was being made via AJAX, through the aptly named IsAjaxRequest() method which was an available method on the Request object, as shown below: public ActionResult YourActionName() { // Check if the request is an AJAX call var isAjax = Request.IsAjaxRequest();
So by using ajax () method in Laravel application, We can check a request is ajax or not in controller. In Laravel Request class has many methods to read as HTTP request for the current request. So we can also check if request is over https or request has json content type.
Register ajaxStop () event handler which execute when all AJAX request is being completed and display the message on the screen that hides after 4 seconds using setTimout (). 2. Demo 1 Check the checkboxes following with row and click the Delete button. 3. Disable completion detecting
This event handler executes when all AJAX requests are being completed. It also triggers when an AJAX request is canceled. Loop all checked checkboxes and sending AJAX request which will remove <table> row when successfully deleted.
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.
The best mehtod is :
if (!$this->getRequest()->isAjax()) {
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With