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:
Best Answer
Instead of
$this->getUrl()
, you can tryPlease note the method is
Ajax
and notajax
. This is because your controller consists of a methodAjaxAction()
and it is notajaxAction()
.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 insideMage
and it is a static function. So you need to call that method like thisMage::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 whyMage::helper('adminhtml')->getUrl()
worked.Mage::helper('adminhtml')
will return admin helper class. More specifically it return the classMage_Adminhtml_Helper_Data
which is located atapp/code/core/Mage/Adminhtml/Helper/Data.php
. If you look in this file, you can find the methodgetUrl()
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 whyMage::getUrl()
didn't work in this case I suspect. This is becauseMage::getUrl()
usesMage_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