Jump to content

Handling Ajax in Prestashop 1.7.5


Recommended Posts

Hello. Im putting into practice a tutorial I found in a project. I need to make an Ajax request and recive a variable from the the php file, but instead I recieve the html data of the website. Can anyone help me finding a solution?

This is my js file.

function change1(link)
{
console.log("Ajax: OK");
console.log('....');

$.ajax({
  type: 'POST',
  dataType : 'json',
  url: link,
  cache: false,
  data: {
  variable_1 : 'test',
  ajax: true,
  },
  success: function (data, status) {
  console.log(status);
  console.log(data);
  },
  error : function (hx, status)
  {
  console.log(status);
  }
});
}

This is my target php file for the ajax

<?php
    class MycustommoduleAjaxModuleFrontController extends ModuleFrontController
    {

        public function initContent()
        {
            $this->ajax = true;
            parent::initContent();
        }

        public function displayAjaxdoSomeAction()
        {
            $var1 = Tools::getValue('variable_1');
            echo "<script>console.log(".$var1.")</script>";
            die(Tools::jsonEncode(array('result' => "test")));
        }

    }
?>

This is the result in the console log.

image.png.cb9bc43eb897caa0eaa14fec68f9d7bd.png

As you can see, the returned data is a lot of html and not necessary the result => test, I was hoping for.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...