2

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?

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

| improve this answer | |
2

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>
                    ');
| improve this answer | |
0

You can get URL using below code.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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