Magento 1 AJAX – How to Call a Controller Function

ajaxmagento-1url

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

Best Answer

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

Related Topic