我在我的控制器中有一个功能来制作搜索过滤器。
搜索参数是从我的树枝模板的选择字段中获取的。
将所选选项传递给控制器,以查看该值的结果。
查询结果以JSON格式返回。
控制器:
public function categoryAction(Request $request)
{
$category = $request->request->get('category');
$contentCategory = $em->getRepository('MyAppBundle:Content')->findByCategory($category);
$filterContent = new JsonResponse();
$filterContent->setData([
'categoryResult' => $contentCategory
]);
return $filterContent;
}
Twig模板:
$('#selectCategory').change(function() {
var optionSelect = $(this).val();
$.ajax({
url: '{{path('playlist_category') }}',
data: '&category='+optionSelect,
type: 'POST',
success: function(filterContent) {
},
error: function(e){
console.log(e.responseText);
}
});
});
如何在我的函数'success'中显示JSON返回的结果?
答案 0 :(得分:0)
您应该将JS代码更改为以下内容。我希望用户$.post
而不是$.ajax
。并且不要忘记将json
作为第四个结果传递。
$('#selectCategory').on('change', function () {
var optionSelect = $(this).val();
$.post("{{path('playlist_category') }}", {category: optionSelect}, function (res) {
console.log(res);
}, 'json');
});