I'm writing a module to carry out a simple Ajax call in Magento, but I'm unable to get it work thus far - I feel like I'm missing a vital component somewhere. These are the files I currently have:
Creare/Groupedajax/controllers/AjaxController.php:
class Creare_Groupedajax_AjaxController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
}
Creare/Groupedajax/etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<Creare_Groupedajax>
<version>0.1.0</version>
</Creare_Groupedajax>
</modules>
<frontend>
<routers>
<groupedajax>
<use>standard</use>
<args>
<module>Creare_Groupedajax</module>
<frontName>groupedajax</frontName>
</args>
</groupedajax>
</routers>
<layout>
<updates>
<groupedajax>
<file>groupedajax.xml</file>
</groupedajax>
</updates>
</layout>
</frontend>
</config>
My Ajax Call:
$j.post("groupedajax/ajax/index", { size: $j(this).val()}, function(data) {
$j('#results').html(data);
});
layout/groupedajax.xml:
<?xml version="1.0"?>
<layout version="1.0">
<groupedajax_ajax_index>
<block type="groupedajax/groupedajax" name="root" output="toHtml" template="groupedajax/groupedajax.phtml" />
</groupedajax_ajax_index>
</layout>
My .phtml file simply has 'test' in it at the moment. I just need my results div to return the 'test' value. I just want to know if all the bits are in place for this to work?
This is the tutorial I have followed: http://www.atwix.com/magento/ajax-requests-in-magento/
======================== SOLVED ========================
I just needed a forward slash at the beginning of my url:
$j.ajax({
url: "/groupedajax/ajax/index",
type: "POST",
data: "size="+$j(this).val(),
success: function(data) {
$j('#results').html(data);
}
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…