0

I was facing one big issue to access base URL of store in custom module through jQuery object in magento 2.

In PHP, generally it can obtain using $this->getUrl('module/controller/action'); but in jQuery object how we can obtain same ?

In checkout module we seems, there is usage of this.getUrl() method. And its working fine.

After giving hours of dedication I found the solution. and It will be helpful to you.

asked Jul 11, 2016 at 11:47

1 Answer 1

2

In your jQuery file add 'mage/url' and create a object of it 'urlBuilder'.

e.g.

define(
 [
 'jquery', 
 'underscore',
 'mage/url',
 'jquery/ui'
 ], function (,ドル _,urlBuilder) {
 'use strict';
 getCustomUrl: function() {
 return urlBuilder.build('/routeName/controller/actioName');
 }
});

And use getCustomUrl() function in this jQuery class/widget anywhere.

As per your requirement you can modified it as your best.

answered Jul 11, 2016 at 11:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.