我是cs cart的新手。我需要将ajax结果传递给块模板文件。
我的控制器中有以下代码: -
if($mode=='drill')
{
$id=166;
Registry::get('view')->assign('pid', $id);
$all_subcategory=fn_get_subcategories($id);
foreach($all_subcategory as $value){
$sub_cat[$value['category_id']]=$value['category'];
}
Registry::get('view')->assign('all_year', $sub_cat);
Registry::get('view')->display('templates/blocks/product_filters/components/drillsearch.tpl');
Registry::get('view')->assign('all_year', $sub_cat);
return $sub_cat;
}
I have the following code in drillsearch.tpl :-
function fun(){
var sBase = location.href.substr(0, location.href.lastIndexOf("/") + 1);
$.ajax({
url: sBase+"index.php?dispatch=drill.drill",
data: 123 ,
type: "POST",
success: function(data) {
alert("sfd");
alert(data);
},
error: function(xhr, status, error) {
alert('failed');
alert(xhr.responseText);
}
});
}
这里的ajax结果没有分配模板/ blocks / product_filters / components / drillsearch.tpl。如何解决它。请帮帮我
答案 0 :(得分:0)
从视图向控制器发送请求时,您可以回复"通过简单地使用这个来看视图:
Registry::get('ajax')->assign('all_year', $sub_cat);
我相信这应该可以正常工作,因为您需要的变量可用于您的AJAX响应。
答案 1 :(得分:0)
请尝试
if($mode=='drill'){
$id=166;
$all_subcategory=fn_get_subcategories($id);
foreach($all_subcategory as $value){
$sub_cat[$value['category_id']]=$value['category'];
}
header('Content-Type: application/json');
echo json_encode($sub_cat);
exit;
}
function fun(){
$.ajax({
url: Tygh.index_script+"?dispatch=drill.drill",
data: { variable: '123' },
type: "GET",
success: function(categories) {
for(var category_id in categories){
console.log(category_id+' > '+categories[category_id]);
}
}
});
}
我希望有所帮助,