ylli Posted October 23, 2015 Share Posted October 23, 2015 Hello, i want to use ajax to get data from a controller. I have created a module and a front controller inside that module but when i try to get data from the tpl it shows me 404 not found. Can anyone help me to find whats wrong ? My controller is located in modules/ajaxcat/displaycats.php <?php Class ajaxcatDisplaycatsModuleFrontController extends ModuleFrontController { public function initContent() { parent::initContent(); $this->ajax = true; // enable ajax } public function displayAjax() { if ($this->errors) die(Tools::jsonEncode(array('hasError' => true, 'errors' => $this->errors))); else { switch (Tools::getValue('method')) { case 'getFirstCats' : die(Tools::jsonEncode(array('result' => "test"))); break; default: exit; } exit; } } ?> and the javascript code that i want to get data from this controller is : $.ajax({ type: 'get', url: baseUri+'modules/ajaxcat/displaycats', data: 'method=getFirstCats', dataType: 'json', success: function(json) { console.log(json); alert(json.result); } }); when i execute javascript code it shows me not found. How do i have to write the module url so i can access the controller? Thanks. Link to comment Share on other sites More sharing options...
yaniv14 Posted October 24, 2015 Share Posted October 24, 2015 I think you should put the module controller inside controller/front folder and setup controller in your module __construct function like: $this->controllers = 'displaycats'; and in the tpl file you can do: {$link->getModuleLink('ajaxcat', 'displaycats', [], true)|escape:'html':'UTF-8'} to create the controller url. Also you can manually copy the controller file to PS root controller/front folder and setup the url from the back office under Preferences -> SEO & Url 1 Link to comment Share on other sites More sharing options...
haunter Posted September 18, 2019 Share Posted September 18, 2019 Die is bad solution. Use Symfony\Component\HttpFoundation\JsonResponse; in your Symfony based controller. Link to comment Share on other sites More sharing options...
RickHughson Posted September 20, 2019 Share Posted September 20, 2019 To get data in Ajax from the controller to the view, I use that code : $(document).ready(function() { $("#choix_contact").change(function () { var selectContact = document.getElementById('choix_contact'); var index = selectContact.value; var url = "/Accueil/GetContactInfo"; $.getJSON(url, { indexContactList: index }, function (data) { $("#nom").html(data.Nom); $("#prenom").html(data.Prenom); $("#email").html(data.Email); $("#fonction").html(data.Fonction); $("#telFix").html(data.TelephoneFixe); $("#telPort").html(data.TelephonePort); }); }); }); Maybe someone has any idea about dissertation methodology help services? Link to comment Share on other sites More sharing options...
philipp.haugg Posted October 10, 2019 Share Posted October 10, 2019 Hi, Probably the URL you are sending in your Ajax request is not correct: So, try to create the URL like below and then use the same in your JS code. $link = new Link(); $ajax_url = $link->getModuleLink('ajaxcat', 'displaycats'); 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