1

app/etc/modules/Custom_Customajax.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Customajax>
             <active>true</active>
             <codePool>local</codePool>
        </Custom_Customajax>
    </modules>
</config>

app/code/local/Custom/Customajax/controllers/AjaxController.php

<?php
class Custom_Customajax_AjaxController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->renderLayout();
    }
}

app/code/local/Custom/Customajax/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Custom_Customajax>
      <version>0.1.0</version>
    </Custom_Customajax>
  </modules>
  <frontend>
    <routers>
      <Customajax>
        <use>standard</use>
        <args>
          <module>Custom_Customajax</module>
          <frontName>customajax</frontName>
        </args>
      </Customajax>
    </routers>
    <layout>
      <updates>
        <customajax>
          <file>customajax.xml</file>
        </customajax>
      </updates>
    </layout>
  </frontend>
</config>

mytemplate/layout/customajax.xml

<?xml version="1.0"?>
<layout version="1.0">
  <customajax_ajax_index>
    <block type="customajax/customajax" name="root" output="toHtml" template="customajax/customajax.phtml" />
  </customajax_ajax_index>
</layout>

mytemplate/template/customajax/customajax.phtml

<?php

echo 'test';

Ajax call

$('.submit-form').click(function(e) {
    e.preventDefault();
    $.ajax({
        url: "/mysite/customajax/ajax/index",
        type: "POST",
        success: function(data) {
            if(data.success) {
                $('#results').html(data.message);
            }
        }
    });
});
1

1. Change your module's router configuration like this.

       <routers>
          <customajax>
            <use>standard</use>
            <args>
              <module>Custom_Customajax</module>
              <frontName>customajax</frontName>
            </args>
          </customajax>
       </routers>

Note the change that you made here is using customajax instead of Customajax. Now Magento will recognize your customajax_ajax_index layout handle.

2. Make sure your custom block exist. As per the definition in your layout XML file, Magento needs the following block class to be defined inside your module.

FILE : app\code\local\Custom/Customajax/Block/Customajax.php

<?php
class Custom_Customajax_Block_Customajax extends Mage_Core_Block_Template
{
}

Job is not over. Eventhough we defined our custom block, Magento do not know where exactly it needs to look for our custom block. So we need to tell to Magento that, "she" should look in our own module. For this add this code snippet in config.xml

<global>
    <blocks>
        <custom_customajax>
            <class>Custom_Customajax_Block</class>
        </custom_customajax>
    </blocks>
</global>

3. I am not sure about this point. You need to set url keyword like this

    url: "www.domain.com/customajax/ajax/index",

where www.domain.com is your site's base url.

You are done. Now clear the cache and then try again

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.