Magento Admin – Running AJAX Script from Admin Area

adminajax

I'm trying to make an AJAX call from the sales order create index in the Magento backend. My url is formed by this function and is returning a 404.

This is the URL it is forming
http://127.0.0.1/wsi/index.php/admin/autocustomerlookup/address/checkEmailExists/key/e7dc1ffa2f80b1892da02f80688a29c4/

<?php echo Mage::helper('adminhtml')->getUrl('autocustomerlookup/address/checkEmailExists');?>

I'm basing my work on this Git repo https://github.com/zhukandrey/Atwix_EmailAjax/

I can't figure out why it isn't working, I am certain there is some error with my controller or my routing, trying to make the call from the Magento backend. But I'm stumped!

<config>
    <modules>
        <TwilitGrotto_AutoCustomerLookup>
            <version>1.0.0.0</version>
        </TwilitGrotto_AutoCustomerLookup>
    </modules>
    <global>
        <helpers>
            <twilitgrotto_autocustomerlookup>
                <class>TwilitGrotto_AutoCustomerLookup_Helper</class>
            </twilitgrotto_autocustomerlookup>
        </helpers>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <twilitgrotto>
                    <file>twilitgrotto_autocustomerlookup_layout.xml</file>
                </twilitgrotto>
            </updates>
        </layout>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <twilitgrotto_autocustomerlookup>
                        <use>admin</use>
                        <args>
                            <module>TwilitGrotto_AutoCustomerLookup</module>
                            <modules>
                                <TwilitGrotto_AutoCustomerLookup after="Mage_Adminhtml">
                                    TwilitGrotto_AutoCustomerLookup
                                </TwilitGrotto_AutoCustomerLookup>
                            </modules>
                            <frontName>autocustomerlookup</frontName>
                        </args>
                </twilitgrotto_autocustomerlookup>
            </adminhtml>
        </routers>
    </admin>
</config>
function handleEmailEvent(){
    var email = document.getElementById("email");
    var url = document.getElementById('check_email_address_exists').innerHTML;
    makeRequest(url, $F(email) ); 
}

function makeRequest(url, emailAddress){

    var httpRequest = new XMLHttpRequest();

    console.log(url);

    if ( !httpRequest) {
        console.log('httpRequest failed');
        return false;
    }

    httpRequest.open( 'POST', url );
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpRequest.send('emailAddress=' + encodeURIComponent(email));

    console.log(httpRequest);
}
class TwilitGrotto_AutoCustomerLookup_AddressController extends Mage_Adminhtml_Controller_Action{

    public function checkEmailExistsAction(){

        $emailAddress = $this->getRequest()->getParam('emailAddress');
        $result = Mage::helper('twilitgrotto_autocustomerlookup')->checkEmailExists($emailAddress); 
        $this->getResponse()->setBody($result);

    }

}
class TwilitGrotto_AutoCustomerLookup_Helper_Data extends Mage_Core_Helper_Data{

    public function checkEmailExists($emailAddress) {

        $emailAddressCheck = Mage::getModel('customer/customer')
            ->getCollection()
            ->addAttributeToSelect('email')
            ->addAttributeToFilter('email', $emailAddress)->load();

        if(!$emailAddressCheck->getSize()) {
            $result = 'ok';
        } else {
            $result = 'error';
        }

        return $result;
    }

}

Edited to add entire config.xml

Best Answer

Looks like your admin route declaration is incorrect. Try this:

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <TwilitGrotto_AutoCustomerLookup after="Mage_Adminhtml">TwilitGrotto_AutoCustomerLookup</TwilitGrotto_AutoCustomerLookup>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

Here is a good article on the topic: Magento Admin Hello World Revisited

EDIT:

I'm posting a working solution below:

File: TwilitGrotto\AutoCustomerLookup\etc\adminhtml.xml

<?xml version="1.0"?>
<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <sales>
                        <children>
                            <twilitgrotto_autocustomerlookup translate="title" module="twilitgrotto_autocustomerlookup">
                                <title>Auto Customer Lookup</title>
                            </twilitgrotto_autocustomerlookup>
                        </children>
                    </sales>
                </children>
            </admin>
        </resources>
    </acl>
</config>

File: TwilitGrotto\AutoCustomerLookup\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <TwilitGrotto_AutoCustomerLookup>
            <version>1.0.0.0</version>
        </TwilitGrotto_AutoCustomerLookup>
    </modules>
    <global>
        <helpers>
            <twilitgrotto_autocustomerlookup>
                <class>TwilitGrotto_AutoCustomerLookup_Helper</class>
            </twilitgrotto_autocustomerlookup>
        </helpers>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <twilitgrotto>
                    <file>twilitgrotto_autocustomerlookup_layout.xml</file>
                </twilitgrotto>
            </updates>
        </layout>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <TwilitGrotto_AutoCustomerLookup after="Mage_Adminhtml">TwilitGrotto_AutoCustomerLookup_Adminhtml</TwilitGrotto_AutoCustomerLookup>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

File: TwilitGrotto\AutoCustomerLookup\controllers\Adminhtml\Autocustomerlookup\AddressController.php

<?php

class TwilitGrotto_AutoCustomerLookup_Adminhtml_Autocustomerlookup_AddressController
    extends Mage_Adminhtml_Controller_Action
{
    public function checkEmailExistsAction()
    {
        $emailAddress = $this->getRequest()->getParam('emailAddress');
        $result = Mage::helper('twilitgrotto_autocustomerlookup')->checkEmailExists($emailAddress);
        $this->getResponse()->setBody($result);
    }

    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('sales/twilitgrotto_autocustomerlookup');
    }
}

Controller URL: Mage::getUrl('adminhtml/autocustomerlookup_address/checkEmailExists');