2013-06-04 28 views

回答

1

OcJoy是正確的方式,但提供的解決方案將包含我猜想所有的頁面數據都不是你想要的輸出。然後打http://www.example.com/index.php?route=product/category&path=20&json只有JSON的產品類別陣列ID 20後

if(isset($this->request->get['json'])) { 
    echo json_encode($this->data['products']); 
    die; 
} else 

而是他的解決方案,您應該$this->response->setOutput($this->render());之前做這樣的事情(假設你只需要的產品JSON陣列)將被輸出。


編輯:從模板作爲一個AJAX請求中調用該方法,你可以這樣做:

$(document).ready(function(){ 
    $.ajax({ 
     url: 'index.php?route=product/category&path=<CATEGORY_ID>&json', 
     type: 'get', 
     dataType: 'json', 
     beforeSend: function() { 
     }, 
     complete: function() { 
     }, 
     success: function(data) { 
      if (data.length > 0) { 
       // do something here with the products in data array 
      } 
     } 
    }); 
}); 

確保使用正確的值來代替<CATEGORY_ID>在以上網址,並添加success回調函數中所需的功能。

+0

嘿非常感謝你的例子,如果有可能你會介意給我舉一個例子,如何用.ajax來使用它。我以前做過,但只消費jsonp,我知道jQuery在我的url後添加了一個?回調。我如何從這裏獲得數據? –

+0

@DaimanLoks你是指通過從AJAX調用這個函數是什麼意思?像從哪個模板(頁面)? OpenCart充滿了AJAX調用,您可以從例如'catalog/view/theme/default/template/account/register.tpl'或'.../template/product/product.tpl' - 應該有幾個'jQuery.load()'或'jQuery.post ()'調用...如果在找到合適的示例時遇到問題,請告訴我。 – shadyyx

+0

對不起,我的意思是,使用jQuery .ajax方法調用它。正如你所看到的,當涉及到這種技術時,我並不是非常狡猾。 –

1

在目錄/控制器/產品/ catalog.php前

$this->response->setOutput($this->render()); 

粘貼此代碼

if (isset($this->request->get['json'])) $this->response->setOutput(json_encode($this->data)); else  

,去鏈接http://opencart.examle/index.php?route=product/category&path=20&json

+1

我懷疑這會工作或返回OP所要求的相關響應... – shadyyx

+0

我特別檢查了不同版本1.5.x上的代碼。如果你替換'$ this-> response-> setOutput($ this-> render()); (this)(isset($ this-> request-> get ['json']))$ this-> response-> setOutput(json_encode($ this-> data)); else \t $ this-> response-> setOutput($ this-> render());' – OcJoy

+0

您可以通過我的解決方案獲得完整的json響應,包括麪包屑,分頁,語言,產品和分類信息。 – OcJoy

相關問題