In my magento admin form, I have added a category field. Based on the selection of category in category field an ajax call should be done.
$category = $fieldSet->addField(
'category_id', 'select', [
'name' => 'category_id',
'label' => __('Category'),
'required' => true,
'values' => $this->_selectedCategories->toOptionArray(),
]
);
I have set ajax call for that field
$url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl');
$category->setAfterElementHtml('
<script>
require([\'jquery\'], function($){
var customUrl =
$("#category_id").on(\'change\', function () {
$.ajax({
url: \''.$url.'\',
type: \'POST\',
data: {category_id: $(this).val(), form_key: window.FORM_KEY},
success: function (response) {
$("#url").val(response[\'content\']);
}
});
});
});
</script>
');
Now how to set url for ajax call?
url: \'<?= $url; ?>\'
I am not getting controller call using this line. How to set $url as a ajax call url in script?