PrestaShop后端,ajax调用中的致命错误

时间:2017-03-31 10:05:26

标签: php ajax prestashop prestashop-1.6

在PrestaShop网站的后端我使用此功能:

public function hookAjax($action, $id_product, $id_lang, $title, $descript, $order, $id = NULL)
{

    /* various code*/

    $this->context->smarty->assign(
        array(
            'block_define'  => $this->getFormDesc($id_product)
        )
    );
    return $this->context->smarty->fetch($this->local_path.'views/templates/hook/admin_extra_desc.tpl');
}

public function getFormDesc($id_product) {
    $array = array();

    foreach (Language::getLanguages() as $lang) {
        /*various code*/

        foreach($result as $k=> $r) {
            $files   = array();
            $helper = new HelperImageUploader();
            $helper->setMultiple(false)->setUseAjax(true)->setName('thumbnail_'.$r['id'].'_'.$r['id_lang'])->setFiles($files)->setMaxFiles(3)->setUrl('../modules/module-name/imgAjaxCall.php?');
            $result[$k]['img-form'] = $helper->render();
            $result[$k]['img'] = $result[$k]['img'] ? _PS_BASE_URL_.__PS_BASE_URI__.'modules/module-name/upload/'.$result[$k]['img'] : '';
        }

        $array[$lang["id_lang"]] = array(
            'lang_data'     =>  $lang,
            'count'         =>  count($result),
            'data'          =>  $result
        );
    }

    return $array;

}

HookAjax由:

调用
<?php 
    include(dirname(__FILE__).'/../../config/config.inc.php');


    $context = Context::getContext();
    $addDesc = Module::getInstanceByName('module-name');

    echo $addDesc->hookAjax($_POST['action'],$_POST['id_prodotto'],$_POST['lang'],$_POST['title'], $_POST['text_desc'], NULL, $_POST['row']);

?>

但我对这个错误感到困惑:

致命错误:在第257行的{my_site} /classes/helper/HelperUploader.php中的非对象上调用成员函数addJs()

1 个答案:

答案 0 :(得分:1)

在包含init.php之后,您需要在HookAjax中包含config.inc.php,以便在上下文中初始化控制器。

include(dirname(__FILE__).'/../../init.php');

请注意,这只是不好的做法,尊重MVC并使用适当的控制器进行AJAX调用和数据验证/处理,而不是在主模块类中。