Magento 2 add form key to Ajax URL

When you do an Ajax call in the admin for example adding some button to the system configuration page which calls some URL by Ajax then you have to insert the form key to that URL otherwise it will not call the controller successfully by Ajax.

How to add form key to Ajay URL.

Use the below script to create the URL with the form key in Magento 2

public function getAjaxUrl()
{
    return $this->_urlBuilder->getUrl(
	'dhairvitest/system_config/validatekey',
	[
	    'form_key' => $this->getFormKey();
	]
    )
}