从 Prestashop 模块发出 ajax 请求 [英] Make an ajax request from a Prestashop module

查看:44
本文介绍了从 Prestashop 模块发出 ajax 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个模块,我需要制作一个 ajax 请求,如果可能的话,使用 JSON 响应,我该怎么做?我不太了解 Prestashop 1.7 在这方面的结构.

I am making a module and I need to make an ajax request, with JSON response if possible, how can i do this ? I don't understand really well the structure of Prestashop 1.7 on this.

谢谢!

推荐答案

这很简单,您只需要按照 Prestashop 的标准制作控制器,然后将其链接到您的前端 Javascript.

This is pretty simple, you just have to make the controller with Prestashop's standards then link it to your frontend Javascript.

像这样命名一个 php 文件:./modules/modulename/controllers/front/ajax.php

Name a php file like this : ./modules/modulename/controllers/front/ajax.php

然后放入:

<?php

// Edit name and class according to your files, keep camelcase for class name.
require_once _PS_MODULE_DIR_.'modulename/modulename.php';

class ModuleNameAjaxModuleFrontController extends ModuleFrontController
{
    public function initContent()
    {

        $module = new ModuleName;

        // You may should do some security work here, like checking an hash from your module
        if (Tools::isSubmit('action')) {

            // Usefull vars derivated from getContext
            $context = Context::getContext();
            $cart = $context->cart;
            $cookie = $context->cookie;
            $customer = $context->customer;
            $id_lang = $cookie->id_lang;

            // Default response with translation from the module
            $response = array('status' => false, "message" => $module->l('Nothing here.'));

            switch (Tools::getValue('action')) {

                case 'action_name':

                    // Edit default response and do some work here
                    $response = array('status' => true, "message" => $module->l('It works !'));
                    
                    break;

                default:
                    break;

            }
        }

        // Classic json response
        $json = Tools::jsonEncode($response);
        echo $json;
        die;

        // For displaying like any other use this method to assign and display your template placed in modules/modulename/views/template/front/...
        // Just put some vars in your template
        // $this->context->smarty->assign(array('var1'=>'value1'));
        // $this->setTemplate('template.tpl');

        // For sending a template in ajax use this method
        // $this->context->smarty->fetch('template.tpl');

    }
}

?>

在你的 Module Hooks 中,你需要在 JS 中访问路由,所以我们基本上创建一个变量:

In your Module Hooks, you need to bring access to the route in JS, so we basicaly make a variable :

// In your module PHP
public function hookFooter($params)
{
    
    // Create a link with the good path
    $link = new Link;
    $parameters = array("action" => "action_name");
    $ajax_link = $link->getModuleLink('modulename','controller', $parameters);

    Media::addJsDef(array(
        "ajax_link" => $ajax_link
    ));

}

在前端,您只需在 JS 文件中像这样调用它(此处使用 jQuery):

On the frontend side, you just call it like this in a JS file (with jQuery here) :

// ajax_link has been set in hookfooter, this is the best way to do it
$(document).ready(function(){

    $.getJSON(ajax_link, {parameter1 : "value"}, function(data) {

        if(typeof data.status !== "undefined") {

            // Use your new datas here
            console.log(data);

        }

    });

});

瞧,您已经准备好使用 ajax 控制器了

And voila, you have your ajax ready to use controller

这篇关于从 Prestashop 模块发出 ajax 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆