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?

up vote 4 down vote accepted
  1. Your URL should not have a session key. Remove as below.

$url = $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl', ['_nosid' => true]);

  1. Correct the way to add a variable in the string.

$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> ');

  • 1
    Its Working !!!. Thank you. – Pavitra Nov 21 at 5:04

Try to use this below way :

$category->setAfterElementHtml('
                        <script>
                        require([\'jquery\'], function($){
                        var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';
                        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>
                    ');

You can get URL using below code.

var url = '" . $this->_urlInterface->getUrl('homepage/ajax/getcategoryurl') . "';

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.