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?

1
  • could you specify the step where you are using this button. May 24, 2017 at 7:36

1 Answer 1

7

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();
                    }
                );
            }
        });
    }
);
5
  • 1
    How to pass data and retrieve data in controller can you please explain Nov 28, 2017 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. Jun 28, 2018 at 2:50
  • Hello Prince. Do you know about which method use as beforeSend() in storage.post() ? Sep 12, 2018 at 9:46
  • How to get post data in controller while using storage ajax call? Dec 12, 2019 at 7:18
  • Getting this error: cabinet.js:19 Uncaught TypeError: Cannot read properties of undefined (reading 'post') Oct 23, 2021 at 6:12

Your Answer

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

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