duan41497 2017-10-24 14:03
浏览 62
已采纳

Prestashop 1.7控制器中的多个ajax动作功能

I have a javascript file and i have two ajax calls in it on different buttons. I have a controller, Auction.php, in which i have the functions for both actions ( showPopup and askBidControl ), but for the second function, the ajax returns 404 not found. It doesn't even enter the initContent function. Here is a sample of the code:

 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']));
    }

}


The 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 is in views/templates/front/

I don't know exactly how to make the second ajax call work. It just gives 404 error.

UPDATE: It seems it doesn't even go through initContent function of Auction.php on the second ajax request ( askBidController ) and it has 2 calls, one for the url i have in ajax, that returns 302 found, and another call to index.php?controller=404.

  • 写回答

1条回答 默认 最新

  • dongsun2789 2017-10-24 14:14
    关注

    The ajax methods of the PrestaShop controllers require a precise syntax (to be called with the action param):

    displayAjax + myCustomAction

    in your case:
    public function displayAjaxAskBidControl(){}
    should works ;)

    NOTICE: the module controllers should ever extend the ModuleFrontController class and not other controllers.

    In your case in the first ajax call you give the id_product as a param, so the parent controller do the initContent method, in the second call the productController doesn't find the id_product so it redirects

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度