Drupal 11: Detecting An Ajax Request

The request object available inside Drupal has a method called "isXmlHttpRequest" that allows the detection of ajax requests.

/** @var \Symfony\Component\HttpFoundation\Request $request */
$request = \Drupal::request();
$isAjax = $request->isXmlHttpRequest();

Use this check when you want to perform different actions during an ajax request.

Add new comment

The content of this field is kept private and will not be shown publicly.