AyyoubEL Posted January 17, 2017 Share Posted January 17, 2017 (edited) i'am looking for the right way to call an action from my controller (modules/mymodule/front/mymodule.php) using Ajax (js). For example i had this function (TestMyAjax), how call i call it trought ajax. public function TestMyAjax(){echo 'OK';} Edited January 17, 2017 by AyyoubEL (see edit history) Link to comment Share on other sites More sharing options...
tarek.fellah Posted January 17, 2017 Share Posted January 17, 2017 (edited) You have to create a FrontController, defaut.php class ModuleNameDefaultModuleFrontController extends ModuleFrontController { public function initContent(){ $row = array(); parent::initContent(); if(Tools::getValue('ajax')){ $id_product = (int)Tools::getValue("id_product"); if($id_product){ $row['id_product'] = $id_product; $product_informations = Product::getProductProperties($this->context->language->id, $row); } if (isset($product_informations) and $product_informations) $json = array( 'productInformations' => $product_informations ); else $json = array( 'status' => 'error', 'message' => $this->l('Error when getting product informations.') ); } else $json = array( 'status' => 'error', 'message' => $this->l('Error when getting product informations.') ); die(Tools::jsonEncode($json)); } } JS in the TPL var id_product = $('input[name="id_product"]').val(); $.ajax({ type: "POST", url: '{$link->$this->context->getModuleLink('modulename', 'default')}', async: false, cache: false, data: "product_id="+id_product+"&ajax=1", success: function(html){ $("#price").html(html.price); $('#link').html(result.productInformations.link); }, error: function() { alert("ERROR:"); } }); You can see using ajax in admin also in this article http://magento-web.blogspot.com/2017/01/utilisation-dajax-en-front-et-back.html it's in frensh, but i think that the code is clear. Best regards. Edited January 17, 2017 by tarek.fellah (see edit history) 2 Link to comment Share on other sites More sharing options...
Sebatien Posted August 4, 2020 Share Posted August 4, 2020 Hi every one , I would like to consume an external webservice using ajax wich take customer information on registartion (email and password) before registration. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now