Prestashop 1.7 控制器中的多个 ajax 动作函数

Prestashop 1.7 multiple ajax action functions in controller

我有一个 javascript 文件,我在不同的按钮上有两个 ajax 调用。我有一个控制器 Auction.php,其中我有两个操作的功能( showPopup 和 askBidControl ),但对于第二个功能, ajax returns 404 未找到。它甚至没有进入 initContent 函数。这是代码示例:

 class askbidAuctionModuleFrontController extends ProductControllerCore {

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

    public function displayAjaxShowPopup(){
        $type = Tools::getValue('type');
        switch($type){
            case 'bid':

                break;
            case 'ask':

                break;
            default:
                return false;
        }
        $product = $this->getTemplateVarProduct();
        ob_end_clean();
        header('Content-Type: application/json');

        $tpl = _PS_MODULE_DIR_.'askbid/views/templates/front/productPopUp.tpl';
        $this->context->smarty->assign('product', $product);
        $this->context->smarty->assign('type', $type);
        $html = $this->context->smarty->fetch($tpl);
        $this->ajaxDie(Tools::jsonEncode([
            'askbid_popup' => $html,
            'product' => $product,
        ]));
    }

    public function displayAjaxAskBidControl(){
        $type = Tools::getValue('type');
        $price = Tools::getValue('price');
        ob_end_clean();
        header('Content-Type: application/json');
        die('saracie');
//        $this->ajaxDie(Tools::jsonEncode(['trafalet']));
    }

}


JS:

$(document).ready(function(){
    $('.askBidButton').on('click', function () {
        let buttons_container = $("#askbid_buttons");
        let data = {
            'action': 'showPopup',
            'id_product': buttons_container.data('id-product'),
            'id_product_attribute': buttons_container.data('id-product-attribute'),
            'type': $(this).val()
        };
        let url = buttons_container.data('popup-url');
        $.post(url, data, null, 'json').then(function (resp) {
            $('body').append(resp.askbid_popup);
            let productModal = $(`#bidask-modal-${resp.product.id}-${resp.product.id_product_attribute}`);
            productModal.modal('show');
            productConfig(productModal);
            productModal.on('hidden.bs.modal', function () {
                productModal.remove();
            });
        }).fail((resp) => {
            prestashop.emit('handleError', {eventType: 'clickQuickView', resp: resp});
        });
        return false;
    });
    $('body').delegate('#continue_place_askbid', 'click', function(e){
       let type = $(this).val();
       let price = $(this).parent().find('#askbid_price_input').val();
       let data = {
           'action':'askBidControl',
           'type': type,
           'price': price
       };
       let url = $(this).closest('.quickview').data('popup-url');
       console.log(url);
       console.log('trafalet');
       $.ajax({
           url: url,
           data: data,
           success: function(resp){
               console.log('trafaleteeeee');
           },
           error: function(err){
               console.log(err);
           }
       });
       return false;
    });

productPopUp.tpl 在 views/templates/front/

我不知道如何使第二个 ajax 调用有效。它只是给出 404 错误。

更新:它似乎甚至没有在第二个 ajax 请求( askBidController )上通过 Auction.php 的 initContent 函数,它有 2 个调用,一个用于 url我在 ajax 中找到了 returns 302,并且另一个调用 index.php?controller=404.

PrestaShop 控制器的 ajax 方法需要精确的语法(使用操作参数调用):

displayAjax + myCustomAction

你的情况:
public function displayAjaxAskBidControl(){}
应该有效 ;)

注意: 模块控制器应该扩展 ModuleFrontController class 而不是其他控制器。

在你的情况下,在第一个 ajax 调用中,你将 id_product 作为参数,因此父控制器执行 initContent 方法,在第二次调用中,productController 没有'找不到 id_product 所以它重定向