user2814933
user2814933

Reputation: 21

Call a controller in prestashop using ajax

How can I call the method / function in the controller, I have a controller name TestController.php, then Test.tpl and Test.js. Also I am having a hard time on what to put on the url area.

I wanted to fetch the data from the controller using the ajax.

public function ajaxProcessTestMyAjax(){
  echo "test";
}

Javascript

$(document).ready(function(){

 $( ".view" ).click(function(){
    $.ajax({
        url: "",
        data: {
            ajax: true,
            action: "TestMyAjax",
        },
        success: function(output){
            alert(output);
        }           
    });
});

Upvotes: 1

Views: 5441

Answers (2)

Saba
Saba

Reputation: 385

Hope it helps:

$.ajax({
url: 'controller/action',
type: "POST",
datatype: 'json',
data: JSON.stringify({
                val1: "test1",
                val2: "test2"
            }),
success: function (jsonData)

Upvotes: 1

gskema
gskema

Reputation: 3221

Most important thing is to specify controller parameter:

$.ajax({
    type: 'POST',
    headers: { "cache-control": "no-cache" },
    url: baseUri + '?rand=' + new Date().getTime(),
    async: true,
    cache: false,
    dataType : "json",
    data: 'controller=cart&ajax=true&token=' + static_token,
    success: function(jsonData)

You may specify data as a string or an object (Doesn't reaelly matter, but creators of PS seemed to like strings).

When you specify controller PS automatically calls the appropriate controller. controller parameter may be sometimes called tab (in older versions?). Annother important thing is pass down token, which you can later check in your controller with special method (controller inherit it).

Another common way is to make a call to a file, and initialize PrestaShop inside your file. Then call some kind of module method.

Upvotes: 0

Related Questions