Get Backend Base Url in JS file Magento 2?

You can get base URL of the backend/admin panel in javascript(JS) file to fetch Custom URL of a site to call ajax request in backend area.

‘mage/url’ widget is not used to fetch backend base URL in Magento 2.

You need to create a template file using layout XML and define the base URL of backend under the template file.

You can define any Global variable and set your URL in the Global variable.

Create a simple layout XML file to fetch backend URL of the site using JS,

Create default.xml file for global access at admin area or create a specific action layout file for specific page,
Path:  app/code/Rbj/MyModule/view/adminhtml/layout/default.xml

Now you need to create js.phtml file,
Path: app/code/Rbj/MyModule/view/adminhtml/templates/js.phtml

Now you can use Global variable inside your custom js file,

var customUrl = window.customUrl;
You can get the backend URL using the above variable in the js file.

Check Get Base Url in Js file for Frontend Area