Load js file using the requirejs-config.js
app/code/BA/BStore/view/adminhtml/requirejs-config.js
requirejs-config.js
var config = {
map: {
'*': {
loadcatalogs:'BA_BStore/js/loadcatalogs'
}
}
};
app/code/BA/BStore/view/adminhtml/web/js/loadcatalogs.js
define(['jquery'], function ($) {
"use strict";
return function(config) {
$("#division_id").on('change', function() {
var divisionId = $("#b_division_id").val();
$.ajax({
url: config.ajaxUrlValue,
type: "POST",
data: { divisionId:divisionId },
showLoader: true,
cache: false,
success: function(response){
console.log(response);
}
});
});
}
});
app/code/BA/BStore/view/adminhtml/templates/loadjs.phtml
<?php
$viewModel = $block->getViewModel();
$ajaxUrl = $viewModel->getAjaxUrl();
?>
<script type="text/x-magento-init">
{
"*": {
"loadcatalogs": {
"ajaxUrlValue": "<?= __($ajaxUrl)?>"
}
}
}
</script>
app/code/BA/BStore/view/adminhtml/layout/adminhtml_system_config_edit.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="footer">
<container name="basys_load_js" htmlTag="div" htmlClass="basys_load_js">
<block name="basysloadjs.viewmodel" template="BA_BStore::loadjs.phtml">
<arguments>
<argument name="view_model" xsi:type="object">BA\BStore\ViewModel\LoadajaxUrl</argument>
</arguments>
</block>
</container>
</referenceContainer>
</body>
</page>