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);
}
});
extend({ initialize: function () { //#ajaxcall button id selector $('#ajaxcall'). on('click',function(){ if($('#ajaxcallvalue'). val()){ ajaxValue = [ 'ajaxvalue' : $('#ajaxcallvalue'). val(); ]; //#ajaxcallvalue this text box id seletor var serviceUrlCreate,serviceUrl,payload; /** * Save values .
To Pass ajax response from the controller in Magento 2, You need to use Magento\Framework\Controller\Result\JsonFactory Object. Magento\Framework\Controller\Result\JsonFactory used for sending a response from a controller to ajax request. Pass your response in setData() method.
Create a PHTML file index. phtml under app/code/Codextblog/Custom/view/frontend/templates from where we want to initialize our ajax call. $ajaxurl = $block ->getAjaxUrl();
The ajax() method can send all type of http requests. The following example sends http POST request to the server. Example: Send POST Request. $.ajax('/jquery/submitData', { type: 'POST', // http method data: { myData: 'This is my data.'
If your javascript is being output from a .phtml template file then you can use a convenience function to make the URL fully-qualified which will then be the safest way to proceed.
$j.ajax({
url: "<?php echo $this->getUrl('groupedajax/ajax/index') ?>",
type: "POST",
data: "size="+$j(this).val(),
success: function(data) {
$j('#results').html(data);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With