2

I'm inside the Admin area, I'm adding a check inside the products, inside my File phtml write:

<?php $ajaxUrl = Mage::helper('adminhtml')->getUrl('adminhtml/MyCompany_MyModule_AjaxController/check'); ?>

This is real path

\http\app\code\local\MyCompany\MyModule\controllers\ajaxController.php

This is the Function inside file phtml

function check(element) {
    new Ajax.Request('<?php echo $ajaxUrl ; ?>', {
    method:     'get',
    onSuccess: function(transport) {
        alert('Sent notification.');
    },
    onFailure: function(transport) {
        alert("Couldn't send a notification. ");
        console.log(transport);
    }
});}

This is inside my controller:

    public function checkAction()
{
    $value= 'example';
    Mage::app()->getResponse()->setBody($value);
}

and this is my config.xml

    <admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <MyCompany_MyModule before="Mage_Adminhtml">MyCompany_MyModule</MyCompany_MyModule>
                    <MyCompany_MyModule after="Mage_Adminhtml">MyCompany_MyModule_Adminhtml</MyCompany_MyModule>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

Premise, the ajax call works but I always get 404 error back I'm pretty sure the paths are wrong, but I can not understand how to set them up, can you help me? thank you

  • Your controller file name should be AjaxController.php and same for class last word – kunj Jul 24 '18 at 2:59
1

Try this

<admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <MyCompany_MyModule before="Mage_Adminhtml">MyCompany_MyModule_Adminhtml</MyCompany_MyModule>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

And controller file should be at: http/app/code/local/MyCompany/MyModule/controllers/Adminhtml/AjaxController.php

<?php

class MyCompany_MyModule_Adminhtml_AjaxController extends Mage_Adminhtml_Controller_Action
{
    public function checkAction()
    {
        $value= 'example'; Mage::app()->getResponse()->setBody($value);

    }
    protected function _isAllowed() {
        return true;
    }
}

You need to put this code for URL. <?php $ajaxUrl = Mage::helper('adminhtml')->getUrl('adminhtml/ajax/check'); ?>

  • Nothing I wrote the suggested changes but it still does not work, error 404 – Orazio D'Arrigo Jul 24 '18 at 8:12
  • unfortunately nothing has changed, I do not understand – Orazio D'Arrigo Jul 24 '18 at 8:30
  • I have updated answer. with this <?php $ajaxUrl = Mage::helper('adminhtml')->getUrl('adminhtml/ajax/check'); ?> – kunj Jul 24 '18 at 8:41
  • Welcome! Please accept answer so it will help to others. – kunj Jul 24 '18 at 8:53
1

Path http\app\code\local\MyCompany\MyModule\controllers\Adminhtml\AjaxController.php

<?php 
class MyCompany_MyModule_Adminhtml_AjaxController extends Mage_Adminhtml_Controller_Action {
public function checkAction()
{
    $successCode = 1;
    Mage::app()->getResponse()->setBody($successCode);
} 
}

Config.xml

            <adminhtml>
            <args>
                <modules>
                    <MyCompany_MyModule before="Mage_Adminhtml">MyCompany_MyModule_Adminhtml</MyCompany_MyModules>
                </modules>
            </args>
        </adminhtml>

Ajax Call

<?php $ajaxUrl = Mage::helper('adminhtml')->getUrl('adminhtml/MyCompany_MyModule_AjaxController/check'); ?>

function check(element) {

new Ajax.Request('<?php echo $ajaxUrl; ?>', {
    method:     'get',
    onSuccess: function(transport) {
        alert('Sent notification.');
    },
    onFailure: function(transport) {
        alert("Couldn't send a notification. ");
        console.log(transport);
    }
}); }

URL Generated

https://localhost/index.php/admin/MyCompany_MyModule_AjaxController/check/key/b800cc33b4822a6b866056ef0094cee4/?isAjax=true&&form_key=emruvSXuKs9r7Bic

Status Code 404

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.