Views: 892
Last Modified: 18.01.2022

How to use component parameters in AJAX-action?

Frequently, AJAX-query must retrieve the same parameters from component that were used for its page display.

  • You need to describe parameters to be used in the method listKeysSignedParameters
          class ExampleComponent extends \CBitrixComponent implements \Bitrix\Main\Engine\Contract\Controllerable
          {
          	protected function listKeysSignedParameters()
          	{
          		//list parameter names to be used in AJAX-actions				
          		return [
          			'STORAGE_ID',
          			'PATH_TO_SOME_ENTITY',
          		];
          	}
  • Get signed template parameters and, for example, pass into your into your component JS class
          <!--template.php-->
          <script type="text/javascript">
          	new BX.ExampleComponent({
          		signedParameters: '<?= $this->getComponent()->getSignedParameters() ?>',
          		componentName: '<?= $this->getComponent()->getName() ?>'
          	});
          </script>
  • Call BX.ajax.runComponentAction (as in examples) with parameter signedParameters.
          BX.ajax.runComponentAction(this.componentName, action, {
          	mode: 'class',
          	signedParameters: this.signedParameters, //here is the way to pass parameters to component.
          	data: data
          }).then(function (response) {
          	//some work
          });

As the result, your can use the parameters STORAGE_ID, PATH_TO_SOME_ENTITY in your AJAX-action. These parameters are signed and integrity is controlled by kernel.

When you need to work with signed parameters inside ajax.php, use the method Controller::getUnsignedParameters() inside the controller action; it will contain array with unpacked data.

Additional information



Courses developed by Bitrix24