2

I want to send AJAX request to the REST API on server from popup in adminhtml and I need "Base URL" value to construct complete URL. I see the following code on the HTML page:

<html><head>
    <script>
        var BASE_URL = 'http://host.com/admin/catalog/';
        var FORM_KEY = 'aAopAjLMymxMN17J';
        var require = {
            "baseUrl": "http://host.com/pub/static/adminhtml/Magento/backend/en_US"
        };
    </script>
</head></html>

I can remove /admin/catalog/ from global BASE_URL and get the target value, but is there a better way to get Base URL value in JS in adminhtml?

This global variable does not present on the frontend:

<html><head>
    <script>
        var require = {
            "baseUrl": "http://host.com/pub/static/frontend/Magento/luma/en_EN"
        };
    </script>
</head></html>

Does exist a method to get Base URL value in JS that is common for both adminhtml & frontend?

1 Answer 1

1

Put this code line in your block:

  • You should try $block->getBaseUrl().
  • Get base url admin : $block->getUrl('adminhtml') => http://host.com/admin
3
  • I have no server side PHP block. All I have are: (1) JS module (./Vendor/Module/view/base/web/js/...) and (2) HTML template that is loaded by this JS module (./Vendor/Module/view/base/web/template/...). This code is embedded into adminhtml page as ui-component (xml file in ./Vendor/Module/adminhtml/ui_component/)
    – Alex Gusev
    Commented Oct 12, 2016 at 13:46
  • In your Ui component, there are Url actions? magento.stackexchange.com/questions/139424/… Commented Oct 12, 2016 at 13:52
  • Yes, I have an actions links in the grid: <listing> <columns> <actionsColumn name="..." class="..."> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="component" xsi:type="string">Vendor_Module/js/grid/columns/actions</item> </item> </argument> </actionsColumn> </columns> </listing>
    – Alex Gusev
    Commented Oct 12, 2016 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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