How to call Drupal AJAX programmatically

There are many ways to make AJAX requests, but because Drupal has its own API, you may want to run that part without any hacks - like AJAX commands and behaviors.

Here is a quick example, of how to call Drupal AJAX programmatically:

var arguments = { value_1: '', value_2: '', value_3: '' }; var ajaxSettings = { url: '/ajax-path', submit: arguments }; Drupal.ajax(ajaxSettings).execute().then(function (data) { // Execute things after ajax request. });

This will post three values to /ajax-path and execute all returned AJAX commands and applied behaviors. It also provides a Promise to run additional things right after the request is done.

Buy Me a Coffee at ko-fi.com

Add new comment