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.
CAPTCHA
4 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.