1

I have tried url.build to build request url for ajax call in magento 2, it does not work ?

JS File code: ( view/adminhtml/web/js/files.js)

/*global window,requirejs*/
requirejs(['jquery', 'Magento_Ui/js/modal/confirm', 'mage/url'], function ($, confirmation, urlBuilder) {
    'use strict';
    $('body #custom_email_button').on('click', function () {
        var requestUrl = urlBuilder.build('productpricequote/email/send');
        console.log(requestUrl);
    });
});

Output:

'productpricequote/email/send'

Any help much appreciated!

Thanks

4
  • 1
    Please show us how you did, so that we can help.
    – P S
    Commented Jan 8, 2018 at 12:58
  • and ouput of requestUrl also
    – P S
    Commented Jan 8, 2018 at 13:01
  • Hello Mr.Kapil, Have you found the solution? If yes, do not forget to share in answer, with us.
    – P S
    Commented Jan 8, 2018 at 17:21
  • same problem, you found a solution?
    – bpoiss
    Commented Apr 25, 2018 at 10:48

5 Answers 5

1

This should work for you.

define(['jquery', 'Magento_Ui/js/modal/confirm', 'mage/url'], 
  function ($, confirmation, urlBuilder) {
   'use strict';

    $('body #custom_email_button').on('click', function () {
        var requestUrl = urlBuilder.build('productpricequote/email/send');
        console.log(requestUrl);
    });

});
2
  • 1
    What is the difference betn answer and question.
    – P S
    Commented Jan 8, 2018 at 13:21
  • requirejs and define difference only, but still does not works. Commented Jan 9, 2018 at 5:54
1

The solution you have implemented in your question is seemed to be accepted by all, but it does not work in fact. So I'm suggesting you some different way:

1. Create a block at app/code/Vendor/Module/Block/YourBlock.php

<?php
namespace Vendor\Module\Block;

class YourBlock extends \Magento\Framework\View\Element\Template
{
    protected $storeManager;
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
    ) {
        $this->storeManager= $storeManager;
    }
    public function getBaseUrl()
    {
        return $this->storeManager->getStore()->getBaseUrl();
    }
}

2. In your template add this script:

<script>
window.baseUrl = <?php echo json_encode($block->getBaseUrl()); ?>
</script>

3. And update your script as follows:

/*global window,requirejs*/
requirejs(['jquery', 'Magento_Ui/js/modal/confirm'], function ($, confirmation, urlBuilder) {
    'use strict';
    $('body #custom_email_button').on('click', function () {
        var requestUrl = window.baseUrl+'productpricequote/email/send'
        console.log(requestUrl);
    });
});

4. Output

YourBaseUrl/productpricequote/email/send
2
  • Thanks, I know this. But I want to generate url in js itself, i don't want to pass anything from phtml file. Commented Jan 8, 2018 at 13:29
  • 1
    Actually Kapil, I had wasted few days searching for the solutioin you are looking for, and ended up implementing this. So best of luck. If you are in rush you can use this way for now and look for your quest later.
    – P S
    Commented Jan 8, 2018 at 13:30
0

Try this use "/admin/productpricequote/email/send"

urlBuilder.build("/admin/productpricequote/email/send");

0

I know, it's not a direct answer to the initial question but I hope it'll help someone.

In case you're looking for a way to pass a URL to a Knockout component that's configured in XML, you just need to use 'url' type instead of 'string'.

<item name="path" xsi:type="url" path="foo/bar/buzz"/>

And you'll get something like

https://yousitename.test/admin/foo/bar/buzz/key/59fc5267a61f37216f082211266f336ff7674b25153d2075efde9c3033fa872f/

An example of a button

<button name="sync" component="Module_Name/js/form/components/your_component_name">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="formElement" xsi:type="string">container</item>
                        <item name="actions" xsi:type="array">
                            <item name="0" xsi:type="array">
                                <item name="targetName" xsi:type="string">${ $.parentName }.sync</item>
                                <item name="actionName" xsi:type="string">redirect</item>
                                <item name="params" xsi:type="array">
                                    <item name="path" xsi:type="url" path="foo/bar/buzz"/>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
                <settings>
                    <title translate="true">Sync</title>

                    <imports>
                        <link name="customerId">${ $.provider }:data.customer_id</link>
                    </imports>
                </settings>
            </button>
-1

I have found solution for this. we just need to add a / before url.

Example:

JS File code: ( view/adminhtml/web/js/files.js)

/*global requirejs*/
requirejs(['jquery', 'Magento_Ui/js/modal/confirm', 'mage/url'], function ($, confirmation, urlBuilder) {
    'use strict';
    $('body #custom_email_button').on('click', function () {
        var requestUrl = urlBuilder.build('/productpricequote/email/send');
        console.log(requestUrl);
    });
});

Output:

'http://siteurl.com/productpricequote/email/send'

Hope this helps! :)

1
  • 3
    The question was url.build does not work for admin url however the output of your answer is for frontend
    – Ashar Riaz
    Commented Sep 22, 2020 at 14:20

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.