creaweb80
creaweb80

Reputation: 13

How to make an admin ajax call in prestashop 1.7.6

I'm trying to make an ajax call in Prestashop Admin:

I created a module without a config page. It just add a button in some backoffice page, I'm trying to make an ajax call to my module file without success.

Making an ajax call in frontend is working (I added an ajax.php file in my modules/mymodule/controller/front/ directory), I tried to do the same thing for admin but it's not working at all.

What I've done:

    "autoload": {
      "psr-4": {
        "MyModule\\Controller\\": "controllers/admin/"
      },
      "config": {
        "prepend-autoloader": false
      },
namespace MyModule\Controller;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;

class DemoController extends FrameworkBundleAdminController
{
    public $auth = false;
    public $ssl = true;
    public $ajax = true;
    public $errors = false;
    public $message;

    public function __construct()
    {
        parent::__construct();
    }
 
    public function initContent()
    {
        parent::initContent();
    }
    
    public function postProcess()
    {
        PrestaShopLogger::addLog("MODULE CONTROLLER OK ", 1);
    }

    public function displayAjax()
    {
            $this->ajaxDie(json_encode(array('success'=> !$this->errors, 'message' => $this->message)));
    }
}

Then I tried to call the ajax from different way in js but never worked (the post query return is a message from prestashop "page not found" with http 200 response.

the doc isn't very helpful and I only find old messages/ways to do (from Prestashop 1.7.5 I'd be able to create a custom Admin controller but it doesn't work), can someone explain me the steps to follow?

thanks

Upvotes: 0

Views: 2848

Answers (1)

Robertino Vasilescu
Robertino Vasilescu

Reputation: 1078

Assuming it is for a PS1.7+ module, using Symphony:

  1. Declare a link in a method of your admin controller (src/Controller/Admin) e.g
$adminLink = $this->generateUrl()

and return in with:

return $this->render
  1. In your views/js/back.js"
$.ajax({
            url: adminLink,
            type: 'POST',
            async: false,
            data: {

            },
            success: (data) => {
            }
        });

Note: check the generateUrl and render functions for the necessary arguments.

Upvotes: 1

Related Questions