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成功的情况下,我看到所有的模板代码的弹出窗口:
我试图改变THIS.VALUE到this.val(),但第一个作品,我可以看到在Chrome网站管理员工具的请求。 我使用OC 1.5。 – Zotoff
更新了答案,我没有意识到你试图让控制器中的产品与ajax对不起 – Matthew
: $ category_id = $ this-> request-> get ['category_id']; get ['category_id'] - 是我通过AJAX发送的请求参数 – Zotoff