Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if request was sent by Ajax or not

Tags:

ajax

magento

I am overriding a magento controller, before processing, is there a way to know if the request was sent by Ajax or not?

Thanks

like image 280
ilyes kooli Avatar asked Jun 06 '12 09:06

ilyes kooli


People also ask

How do I know if AJAX is working?

$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });

How can I tell if AJAX request is complete in asp net?

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.

Which indicates whether or not the specified HTTP request is an AJAX request?

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.

How to check if a request is being made via AJAX?

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();

How to check a request is Ajax or not in Laravel?

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.

How to disable completion detecting when Ajax request is completed?

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

What happens when all Ajax requests are completed in Salesforce?

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.


2 Answers

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

  • Prototype
  • Scriptaculous
  • jQuery
  • YUI
  • MochiKit

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.

like image 199
Jürgen Thelen Avatar answered Nov 15 '22 22:11

Jürgen Thelen


The best mehtod is :

if (!$this->getRequest()->isAjax()) {
   return false;
}
like image 45
A. jouni Avatar answered Nov 15 '22 20:11

A. jouni