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);
 }
 }
 });
});
dotancohen
1,1306 silver badges21 bronze badges
asked Nov 5, 2014 at 4:13

1 Answer 1

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

answered Nov 5, 2014 at 4:52
2
  • 1
    About point 3./ you should consider using $this->getUrl( as said in stackoverflow.com/a/8836722/2375207 Commented Jun 16, 2016 at 13:20
  • @nicolallias yes. I totally agree. But if he is using an external js file, then $this->getUrl() won't help. Commented Jun 17, 2016 at 4:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.