7

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

8
  • 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.
    – mbalparda
    Commented Nov 11, 2014 at 14:21
  • weird, but it gives me the home page of magento, also I changed the name from Attributes_ajaxAction in AjaxAction Commented Nov 11, 2014 at 14:23
  • You are calling an admin controller from the frontend. It is an expected behaviour. See the inheritance of your controller class.
    – mbalparda
    Commented Nov 11, 2014 at 14:25
  • Also If I use the ajax url like this: url: '<?php echo $this->getUrl("adminhtml/attributes/ajax") ?>' in firebug at HTML it gives me the home page Commented Nov 11, 2014 at 14:25
  • I'm not really understanding what you sugested, can you more specific? thx a lot ! Commented Nov 11, 2014 at 14:27

3 Answers 3

6

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

6
  • I'm using this: <?php echo Mage::helper('adminhtml')->getUrl('adminhtml/attributes/ajax'); ?> and it is working , with ajax not Ajax. Why ? Commented Nov 11, 2014 at 15:03
  • 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 :) Commented Nov 11, 2014 at 15:04
  • try Mage::getUrl() and let me know the result Commented Nov 11, 2014 at 15:04
  • I tried also like this: Mage::getUrl('adminhtml/attributes/Ajax'); and im getting the result from the image Commented Nov 11, 2014 at 15:05
  • The only way it works for me is by using Mage::helper('adminhtml')... but don;t ask me why Commented Nov 11, 2014 at 15:07
5

Your request is not reaching your controller because of the way you have defined your controller resource in your config.xml. You are using building a URL with the call:

$this->getUrl('attributes/attibutes_ajax'); // also a typo here, missing 'r', underscore should also be '/'

but you have defined your controller with:

<attin_exportdb before="Mage_Adminhtml">Attin_Exportdb_Adminhtml</attin_exportdb>

The problem lies in the fact that you have not declared a new controller with a frontName, rather used the before syntax to tell Magento to look for a controller match in your module before reverting to Mage_Adminhtml. This is a good way of doing things for admin, but in order to make it work you must target the admin module in your request not your own. This will trigger the logic to check your module for a match first, then revert to Mage_Adminhtml.

So when you are generating the URL you need to be using:

Mage::helper('adminhtml')->getUrl('adminhtml/attributes/ajax');

which will target the admin module, your attributes controller, and the ajax action within that controller. Using the adminhtml helper getUrl() method will include the necessary key in the generated URL (otherwise a request to admin will be rejected).

By using:

Mage::helper('adminhtml')->getUrl('attributes/attributes/ajax');

you would have needed to have delcared your own controller rather than using before and set a frontName similar to the following in your config.xml:

<?xml version="1.0"?>
<config>
    ....
    <admin>
        <routers>
            <exportdb>
                <use>admin</use>
                <args>
                    <module>Attin_Exportdb</module>
                    <frontName>attributes</frontName>
                </args>
            </exportdb>
        </routers>
    </admin>
    ....
</config>
3
  • I used $this->getUrl('adminhtml/attributes/ajax');. Please see the result in the img from my post. Commented Nov 11, 2014 at 14:52
  • I don't understand why, but this url: "<?php echo Mage::helper('adminhtml')->getUrl('adminhtml/attributes/ajax'); ?>" is working for me, can you explain me please ? Commented Nov 11, 2014 at 14:59
  • Frontend and backend URL's are handled completely differently. Backend URL's will virtually always require a key to be added into the URL to validate the request, otherwise it will not be routed. If $this->getUrl() does not target a custom block method which in turn calls the adminhtml helper getUrl() method then you will need to call this method from the template as it will add the required key into the URL. Commented Nov 11, 2014 at 15:16
5

Initiate js in any.phtml file!

jQuery.ajax({
    url: "/module/index/getdata"
    type: "POST",
    data: {key: 'value'},
    success: function(response){
        console.log(response);
    }
});

Write controller app/code/local/Package/Module/controllers/IndexController.php

    Package_Module_IndexContoller extends Mage_Core_Controller_Front_Action{
        public function getdataAction(){
            if($data = $this->getRequest()->getPost("key")){
                echo "data received !";
            }else{
                echo "unable to receive data !";
            }
        }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.