如何在prestashop 1.7中向自定义页面请求ajax

时间:2017-06-02 12:29:37

标签: php ajax prestashop prestashop-1.7

为我糟糕的英语道歉。我是prestashop的新手。请任何人帮忙。如何在prestashop中将AJAX请求发送到自定义php文件

//My js file
     $.ajax({
     url : baseUrl + "modules/<myModule>/ajaxfunc.php",
     type: "POST",
     cache: false,
     data : {form_data: 1 , action:'imageuploadAction'},

     beforeSend: function() {
     $('body').append('<div class="loading_popup">Loading...</div>');},        

    success: function(data){
    console.log(data);
       }
      });

// php file
// modules/<myModule>/ajaxfanc.php

   <?php
   include_once('../../config/config.inc.php');
   include_once('../../init.php');
   class ajaxfuncAjaxModuleFrontController extends ModuleFrontController
    {
    public function  imageuploadAction() {
     die('here');
    }
   }
   ?>

我不知道它是否正确。请指导我。

2 个答案:

答案 0 :(得分:1)

您可以在模块中使用ajax前端控制器,并使用钩子在模块本身中生成Ajax请求所需的URL。

请参阅Make an ajax request from a Prestashop module

答案 1 :(得分:0)

我找到了在prestashop 1.7中获得正确的Ajax请求的解决方案

//在tpl文件中

<script>
  var url= {url entity='module' name='<myModuleName>' controller='<MyControllerName>' params = ['var1' => 1,'var2' => 2,action => 'MyControllerAction']}
</script>

//在Js文件中

$.ajax({
url : url,
type: "POST",
data : 'var3='3,
success : function(response){
  console.log(response);

}   });

//在Controller Php文件中

<?php
  require_once(dirname(__FILE__).'../../../../config/config.inc.php');
  require_once(dirname(__FILE__).'../../../../init.php');
  class <MyModule><MyController>ModuleFrontController extends ModuleFrontController
    {
      public function initContent()
      {
       $this->ajax = true;
        parent::initContent();
      }
      // displayAjax for FrontEnd Invoke the ajax action
      // ajaxProcess for BackEnd Invoke the ajax action

       public function displayAjaxMyControllerAction()
        {
         $var1 = Tools::getValue('var1');
         $var2 = Tools::getValue('var2');
         $var3 = Tools::getValue('var3');

         header('Content-Type: application/json');
         die(Tools::jsonEncode(['var1'=> $var3]);
        }
      }