0

I am trying to get the custom controller in ajax my controller file is located in app/code/local/Minicart/Quick/controllers/minicartcontrolle‌​r.php

and the action name of the controller is add_to_minicart here is my code that i tried to access

  function addtoCart(){
    jQuery("#success_message").show();
    jQuery("#success_message").html('<img src="/demo/media/images/loading-round.gif" alt="Loading..." width="77" height="73">');
    var url;
    url = jQuery('#product_addtocart_form').attr('action');

    url = url.replace("checkout/cart", "ajax/index"); // New Code
    if ('https:' == document.location.protocol) {
        url = url.replace('http:', 'https:');
    }
    var data = jQuery('#product_addtocart_form').serialize();
        data += '&isAjax=1';
    try {
        jQuery.ajax({
            url:url,
            dataType:'json',
            type:'post',
            data:data,
            success:function (data) {
                if(data.status=="SUCCESS"){
            //jQuery("#success_message").html(data.message);
            jQuery("#success_message").html(data.message);
             minicartAjax();
             var qty = jQuery("#qty").val();
             var curqty = jQuery(".header-minicart .count").html();
             var newqty=parseInt(qty)+parseInt(curqty);
             jQuery(".header-minicart .count").html(newqty);
                }
            }

        });
    } catch (e) {

    } 


}
function minicartAjax(){
    var url = "<?php echo Mage::getUrl('quick/controllers/add_to_minicart');?>";
    alert(url);
    jQuery.ajax({
        url: url,
        dataType: 'json',
        success: function(data){
            //Your minicart html
            alert("hii");
        }
    });
}

when i alert the url it just display this <?php Mage::getUrl('quick/controllers/add_to_minicart');?> how can i get the url of my custom controller

2

you forgot an echo before Mage::getUrl.

var url = "<?php echo Mage::getUrl('quick/controllers/add_to_minicart');?>"; 

If you are doing this in a javascript file it won't work. Js files don't interpret <?php ?> tags.

What you can do instead is to change your function to look like this

function minicartAjax(url){
    jQuery.ajax({
        url: url,
        dataType: 'json',
        success: function(data){
            //Your minicart html
            alert("hii");
        }
    });
}

and in the template that calls this javascript, call it like this

minicartAjax('<?php Mage::getUrl('quick/controllers/add_to_minicart');?>')
  • Still have the same problem – amith lal Mar 16 '17 at 9:57
  • I've updated the answer. – Marius Mar 16 '17 at 10:15
  • this function is called from the same js file in my code – amith lal Mar 16 '17 at 10:17
  • there must be something from the template that calls something that calls your function. Pass the url in those functions. – Marius Mar 16 '17 at 10:18
  • @ Marius check my updated code there you can see how i call the function minicartAjax – amith lal Mar 16 '17 at 10:23

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.