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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?