Jump to content
ylli

how to use ajax to get data from controller.

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

  • Like 1

Share this post


Link to post
Share on other sites

Die is bad solution. 

Use Symfony\Component\HttpFoundation\JsonResponse; in your Symfony based controller.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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');
 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More