Prestashop 1.7.6.2 ajax call backoffice module

女生的网名这么多〃 提交于 2021-01-29 05:22:00

问题


I have problem with ajax from select2 in prestashop 1.7. When I try writte something the calling is 200 but I got error "The Controller Psb2BAjaxModuleAdmin is missing or invalid."

I create Controller for test in my module modules/psb2b/src/Controller/Psb2BAjaxModuleAdminController.php

<?php


namespace Scenario\PSB2B\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;

class Psb2BAjaxModuleAdminController extends FrameworkBundleAdminController
{
    public function __construct()
    {
        parent::__construct();
    }
 
    public function initContent()
    {
        parent::initContent();

    return $this->ajaxDie(json_encode("test"));

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

    public function displayAjax()
    {
            $usb_search_token = $this->generateUrl("psb2bAjaxAdmin");

           return $this->ajaxDie(json_encode("test"));
    }

}

and in admin directory admin*********/themes/default/js

$(document).ready(function(){
    $('#category_features').select2({
      width: 'resolve',
      ajax: {
        type: 'POST',
        url: usb_search_token,
        dataType:'json',
        delay: 250,
          data: function (params) {
              return {
                  q: params.term // search term
              };
          },
          success: function (result) {
              console.log(result);
          }
  } });


});

In my module i used hook

public function hookActionAdminControllerSetMedia()
{

    MediaCore::addJsDefL('usb_search_token', $this->context->link->getAdminLink('Psb2BAjaxModuleAdmin'));
    $this->context->controller->addCSS(_PS_BO_ALL_THEMES_DIR_ . 'default/js/select2-full/dist/css/select2.min.css','all');
    $this->context->controller->addJS(_PS_BO_ALL_THEMES_DIR_ . 'default/js/select2-full/dist/js/select2.min.js');
    $this->context->controller->addJS(_PS_BO_ALL_THEMES_DIR_ . 'default/js/tree.js');

}

回答1:


It seems your controller looks more like a 1.6+ one rather than a 1.7 with Symfony one.

I usually have an indexAction method in the controllers/Admin/my_controller.php.

In this method I use a

Media::addJsDef(array(
'usb_search_token' => admin_link));
));

Then as this method returns a

return $this->render('@Modules/rmvcolorgrid/views/admin/my_file.html.twig', [])

the URL is available for the js file in views/js/back.js.

You should have a look at PS docs for the recommended way to build this.



来源:https://stackoverflow.com/questions/65093865/prestashop-1-7-6-2-ajax-call-backoffice-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!