1

We have one button on checkout page to find address from our

API. We want to send one ajax request to that API to get some data.

How do we send ajax request from knockout js?

  • could you specify the step where you are using this button. – Mohammad Mujassam May 24 '17 at 7:36
6

Use mage/storage for ajax call in knockoutjs

define(
    [
        ...
        'mage/storage'
        ...
    ],
    function(
        ...
        storage
        ...
    ) {

        return Component.extend({
            ...

            /** Your function for ajax call */
            myAjaxCall: function(dataToPass) {

                fullScreenLoader.startLoader();
                storage.post(
                    'url/of/mycontroller',
                    JSON.stringify(dataToPass),
                    true
                ).done(
                    function (response) {
                        /** Do your code here */
                        alert('Success');
                        fullScreenLoader.stopLoader();
                    }
                ).fail(
                    function (response) {
                        fullScreenLoader.stopLoader();
                    }
                );
            }
        });
    }
);
  • How to pass data and retrieve data in controller can you please explain – nagendra Nov 28 '17 at 6:43
  • How can i get that data in knockout js bindng returning by this function? my ajax response i returning a string but when i use data-bind="text: myAjaxCall()" instead of printing the stirng, it print object Object. when i debug it by dumping the value of object in console.log, i can see it has the key ResponseJSON which has the string value. but i cant get it. – Zeeshan Khuwaja Jun 28 '18 at 2:50
  • Hello Prince. Do you know about which method use as beforeSend() in storage.post() ? – Rohan Hapani Sep 12 '18 at 9:46
  • How to get post data in controller while using storage ajax call? – Kishan Patadia Dec 12 '19 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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