1

Path of js file-

app/code/Company/Configuration/view/adminhtml/web/js/test.js

code in js file -

define(['jquery', 'mage/validation', 'mage/template'], function ($, _, template) {
    function main(config, element) {
        var $element = $(element);
        var path = config.AjaxUrl;
        $(document).ready(function() {
            $("#lock").click(function(){
                var value1  = $('#value1').val();
                var value2      = $('#value2').val();
                var param = 'ajax=1';
                $.ajax({
                    /*showLoader: true,*/
                    url: path,
                    data: param,
                    type: "GET",
                    dataType: 'json'
                }).done(function (data) {

                    alert(data);


                });
            });
        });
    };
    return main;
});

And code in phtml file to ajax call

<script type="text/x-magento-init">
    {  
        "#lock": {
            "Company_Configuration/js/test":{
                "AjaxUrl":"http://127.0.0.1/magento2/admin/configuration/admin/settings/doSomeAction", "method":"get"
            }
        }
    }        
</script>

Where settings is controller file Settings.php and doSomeAction() is function which I want to call.

Thanks.

Your Answer

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

Browse other questions tagged or ask your own question.