如何在Prestashop模块中使用的Smarty模板中调用JS中的后端函数?

如何在Prestashop模块中使用的Smarty模板中调用JS中的后端函数?,prestashop,smarty,prestashop-1.7,Prestashop,Smarty,Prestashop 1.7,在自定义的prestashop模块中,我尝试在下拉列表中动态显示子类别 这是我在调用模板之前在钩子中添加的代码: <select id="series_dropdown" class="selectpicker" data-style="btn-primary"> {foreach from=$seriesCategories item=seriesCategory} <option value="{$seriesCategory.id_category

在自定义的prestashop模块中,我尝试在下拉列表中动态显示子类别

这是我在调用模板之前在钩子中添加的代码:

<select id="series_dropdown" class="selectpicker" data-style="btn-primary">
    {foreach from=$seriesCategories item=seriesCategory}
        <option value="{$seriesCategory.id_category}">{$seriesCategory.name}</option>
    {/foreach}
</select>
$subcatObj=新类别(“24”);
$subcatObj2=$subcatObj->getSubCategories($this->context->language->id);
$this->context->smarty->assign('seriesCategories',$subcatObj2);
以下是我在模板中使用此选项的方式:

<select id="series_dropdown" class="selectpicker" data-style="btn-primary">
    {foreach from=$seriesCategories item=seriesCategory}
        <option value="{$seriesCategory.id_category}">{$seriesCategory.name}</option>
    {/foreach}
</select>
要做到这一点,我们应该做些什么-它是一种ajaxweb服务-


Prestashop 1.7.1

您需要在模块中创建一个php文件来处理ajax请求,例如:

/modules/your_module/ajax.php


警察局。这是一个基本的示例,您应该在ajax中添加一些安全令牌。

。这真的很有帮助,而且很全面。这个答案很有效,但请使用模块控制器。
$.ajax({
    url: '/modules/your_module/ajax.php',
    type: 'POST',
    dataType: 'JSON',
    data: { id_category: $('#series_dropdown').val() }
})
.done(function(data) {
    console.log(data); // 'data' should contain the response of corresponding sub-categories
})
.fail(function() {
    console.log('An error has occurred!');
});