dtwk6019 2017-03-31 10:05
浏览 52
已采纳

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

On the backend of a PrestaShop site I'm using this function:

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 is called by:

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

?>

But I struggle with this error:

Fatal error: Call to a member function addJs() on a non-object in {my_site}/classes/helper/HelperUploader.php on line 257

  • 写回答

1条回答 默认 最新

  • douhezi2285 2017-04-01 07:50
    关注

    You need to include init.php in HookAjax after including config.inc.php so that controller is initialized in context.

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

    Note that this is just bad practice, respect the MVC and use proper controllers for your AJAX calls and data validation/processing in them, not inside main module class.

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

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏