How do i call a controller function in ajax?

  • This is the content of my .../template/exportdb/attributes.phtml file:

    <span id = "attributes">Generate</span> 
    
    <script type="text/javascript">
        jQuery(function () {
            jQuery("#attributes").click(function(){
                jQuery.ajax({
    
                    url: "<?php echo $this->getUrl('adminhtml/attributes/ajax'); ?>"
    
                }).done(function() {
                    alert("Hey");
                });
            });
        });
    
    </script>
    

    This is my controller file:

    <?php
    class Attin_Exportdb_Adminhtml_AttributesController extends Mage_Adminhtml_Controller_Action{
    
        public function IndexAction() {
    
            $this->loadLayout();
            $this->getLayout()->getBlock("head")->setTitle($this->__("Export Attributes"));
    
            //$this->getLayout()->createBlock('exportdb/adminhtml_attributes');
            //$this->getLayout()->getBlock("exportdb/adminhtml_attributes");
    
            $block = $this->getLayout()->createBlock('Mage_Core_Block_Template','attributes_exp_db',array('template' => 'exportdb/attributes.phtml'));
            $this->getLayout()->getBlock('content')->append($block);
    
            $this->renderLayout(); 
    
        }
        public function AjaxAction(){
            echo "ajax";
        }
    }
    

    This is the config.xml content:

    <?xml version="1.0"?>
    <config>
        <modules>
            <Attin_Exportdb>
                <version>0.1.0</version>
            </Attin_Exportdb>
        </modules>
        <global>
            <helpers>
                <exportdb>
                    <class>Attin_Exportdb_Helper</class>
                </exportdb>
            </helpers>
            <blocks>
                <exportdb>
                    <class>Attin_Exportdb_Block</class>
                </exportdb>
            </blocks>
        </global>
        <admin>
            <routers>
                <adminhtml>
                    <args>
                        <modules>
                            <attin_exportdb before="Mage_Adminhtml">Attin_Exportdb_Adminhtml</attin_exportdb>
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>
    </config>
    

    But the firebug show me the 404, I don't get to Attibutes_ajaxAction function. Why?

    The result: result

    What happens if you access the url directly? I mean via the browser. Im not sure about the _ in the url, try changing the name of the function to ajacAction and call it in the browser to see what happens.

    weird, but it gives me the home page of magento, also I changed the name from Attributes_ajaxAction in AjaxAction

    You are calling an admin controller from the frontend. It is an expected behaviour. See the inheritance of your controller class.

    Also If I use the ajax url like this: url: 'getUrl("adminhtml/attributes/ajax") ?>' in firebug at HTML it gives me the home page

    I'm not really understanding what you sugested, can you more specific? thx a lot !

    It seems you are extending your class from an admin controller. That means it will have all the properties of an admin controller, including the feature of having to be logged in the Magento admin to access it and it seems you are trying to access your controller from the frontend.

    should I use a block instead ?

    See the answer this question just had.

  • Rajeev K Tomy

    Rajeev K Tomy Correct answer

    7 years ago

    Instead of $this->getUrl(), you can try

     Mage::getUrl('adminhtml/attributes/Ajax');
    

    Please note the method is Ajax and not ajax. This is because your controller consists of a method AjaxAction() and it is not ajaxAction().

    Generally this is what Mage::getUrl('module/controller/method') looks like. The fields are self explanatory I hope.

    Why your code didnt work?

    This is because you are calling getUrl() on $this which is wrong. getUrl() function comes inside Mage and it is a static function. So you need to call that method like this Mage::getUrl()

    Wierd that, Mage::getUrl() is not working for you. I will investigate the reason when I get some free time. Now I will answer why Mage::helper('adminhtml')->getUrl() worked.

    Mage::helper('adminhtml') will return admin helper class. More specifically it return the class Mage_Adminhtml_Helper_Data which is located at app/code/core/Mage/Adminhtml/Helper/Data.php. If you look in this file, you can find the method getUrl()

    public static function getUrl($route='', $params=array())
    {
        return Mage::getModel('adminhtml/url')->getUrl($route, $params);
    }
    

    You can see that in case of admin section, magento uses Mage_Adminhtml_Model_Url model class in order to generate the url. This is the exact reason why Mage::getUrl() didn't work in this case I suspect. This is because Mage::getUrl() uses Mage_Core_Model_Url model class in order to generate the url, which is I think generally used for frontend url generation.

    So the big point is Magento uses a special model class Mage_Adminhtml_Model_Url class for processing magento admin urls

    I'm using this: getUrl('adminhtml/attributes/ajax'); ?> and it is working , with ajax not Ajax. Why ?

    and if im not using the helper part Im getting the result (see the image in my post). Can you explain also why? i'm totally confused :)

    try Mage::getUrl() and let me know the result

    I tried also like this: Mage::getUrl('adminhtml/attributes/Ajax'); and im getting the result from the image

    The only way it works for me is by using Mage::helper('adminhtml')... but don;t ask me why

    see my update. I think you will get an idea

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used