2016-05-31 85 views
0

我通過AJAX'get'方法向控制器發送數據時遇到了一些問題。Opencart中的AJAX問題

其選擇從列表

<script> 
    $('#category').on('change', function() { 
      $.ajax({ 
       method: 'GET', 
       url: 'index.php?route=module/my_module&category_id='+this.value, 

      }).done(function(data, Status){ 

      }); 
     }); 
</script> 

我檢查它Ghrome網絡工具的信息

我的AJAX代碼(.tpl文件),它的工作原理,我看到的請求。 我的控制器代碼:

$this->load->model('catalog/product'); 
     $category_id = $this->request->get['category_id']; 
     $this->data['product'] = $this->model_catalog_product->getProductsByCategoryId($category_id); 

     $this->response->setOutput(); 

我不能看到.tpl變量$產品的AJAX請求後。 什麼問題?我試圖在控制器中創建一個特殊的功能,但它不起作用。

UPD: 新的Ajax功能:

$('#category').on('change', function() { 
     $.ajax({ 
     url: 'index.php?route=module/my_module/AjaxGetProduct&category_id=' + this.value, 
     dataType: 'html', 
     success: function(htmlText) { 
     alert(htmlText); 
     $('#product_summary').html(htmlText); 
     }, 
     error: function(xhr, ajaxOptions, thrownError) { 
     alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); 
     } 
    }); 
    }); 

型號代碼:

public function getProductsByCategoryId($category_id) { 
     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2c.category_id = '" . (int)$category_id . "' ORDER BY pd.name ASC"); 

     return $query->rows; 
    } 

    public function getProductDescriptions($product_id) { 
     $product_description_data = array(); 

     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'"); 

     foreach ($query->rows as $result) { 
      $product_description_data[$result['language_id']] = array(
       'seo_title'  => $result['seo_title'], 
       'seo_h1'   => $result['seo_h1'], 
       'name'    => $result['name'], 
       'description'  => $result['description'], 
       'meta_keyword'  => $result['meta_keyword'], 
       'meta_description' => $result['meta_description'], 
       'tag'    => $result['tag'] 
      ); 
     } 

     return $product_description_data; 
    } 

我更新的控制器中的AJAX請求功能:

public function AjaxGetProduct(){ 
     if (isset($this->request->get['category_id'])) { 
      $category_id = $this->request->get['category_id']; 

      if ($category_id > 0) { 
      //loading the AJAX 
       $this->template = 'module/my_module.tpl'; 
       $this->load->model('catalog/product'); 
       $product = $this->model_catalog_product->getProduct($category_id); 
       $data['product'] = $product; 
       $this->response->setOutput($this->render()); 


      } 
      else { 
       echo "Error";   } 
     } else { 
      echo "Error"; 
     } 
    } 

當我嘗試提醒htmlText的值swer在AJAX成功的情況下,我看到所有的模板代碼的彈出窗口: enter image description here

回答

0

它看起來像OC 1.5,並獲得作爲JSON,而不是通過一個模板中刪除$this->template = 'module/my_module.tpl'和改變

傳遞的結果
$data['product'] = $product; 
$this->response->setOutput($this->render()); 

$this->response->setOutput(json_encode($product)); 
+0

我試圖改變THIS.VALUE到this.val(),但第一個作品,我可以看到在Chrome網站管理員工具的請求。 我使用OC 1.5。 – Zotoff

+0

更新了答案,我沒有意識到你試圖讓控制器中的產品與ajax對不起 – Matthew

+0

: $ category_id = $ this-> request-> get ['category_id']; get ['category_id'] - 是我通過AJAX發送的請求參數 – Zotoff