我在通过AJAX“get”方法发送给控制器的数据方面遇到了一些问题。
我的AJAX代码(在.tpl文件中),它从列表中选择信息
<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 Web工具上检查过,它可以工作,我看到了请求。 我在Controller中的代码:
$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();
在AJAX请求之后,我无法在.tpl中看到变量$ product。 有什么问题?我试图在控制器中创建一个特殊功能,但它不起作用。
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;
}
我在Controller中更新了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";
}
}
答案 0 :(得分: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));