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);
}
}
});
});
Best Answer
1. Change your module's router configuration like this.
Note the change that you made here is using
customajax
instead ofCustomajax
. Now Magento will recognize yourcustomajax_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
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
3. I am not sure about this point. You need to set
url
keyword like thiswhere
www.domain.com
is your site's base url.You are done. Now clear the cache and then try again