php - Display results in twig json -
i have in controller function make search filter.
the search parameter obtained select field of twig template.
the selected option passed controller see results value.
the query result returned in json format.
controller:
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 template:
$('#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); } }); });
how can display result returned in json in function 'success'?
you should change js code following. rather user $.post
$.ajax
. , don't forget pass json
fourth arg.
$('#selectcategory').on('change', function () { var optionselect = $(this).val(); $.post("{{path('playlist_category') }}", {category: optionselect}, function (res) { console.log(res); }, 'json'); });
Comments
Post a Comment