i am using symfony 3.4 , i want to execute a controller action on a button click in my twig template , that's why i used ajax for that : first i added the ajax route :
ajax_route:
path: /ajax_request
defaults: { _controller: offerBundle:Default:ajax }
methods: [post]
then the controller action :
public function ajaxAction(Request $request)
{
$personnage = $request->request->get('personnage');
dump($request->request);
$wishlist = new wishlist();
$wishlist->setUserid($personnage);
$wishlist->setOfferid(1);
$em=$this->getDoctrine()->getManager();
$em->persist($wishlist);
$em->flush();
}
the ajax part :
<script>
$('.btn').click( function(){
var personnage = 3;
$.ajax({
url: "{{ path('ajax_route') }}",
type: "POST",
dataType: "json",
data: {
"personnage": personnage
},
async: true,
success: function (data)
{
console.log(data)
}
})
});
</script>
this is my html button :
<button id="wishlist" class="btn">add to wishlist</button>
what happens when i click on the button is i do get this error log :
1 AJAX request Method :POST Type:xhr Status:500 URL:/ajax_request
and the 'var_dump' in the controller dosen't display anything