In my symfony (v3.4) project, I need to pass some javascript variables from my view to my controller : I'm using Jquery and Ajax to send my variable to the controller but I cannot have access to my variables. There is no problem with my Ajax Request, I checked via Symfony profiler and the request is sent correctly but for some reason the controller can't even detect the Ajax request.
Here is my controller:
public function saisieAction(Request $request)
{
$user = $this->getUser();
$thisyear = date("Y");
$em = $this->getDoctrine()->getManager();
// Create the form
$form = $this->get('form.factory')->createBuilder(FormType::class)
->add('ndf', CollectionType::class, array(
'entry_type' => NoteDeFraisType::class,
'label' => false,
'allow_add' => true,
'allow_delete' => true,
))
->getForm();
// if the form has been submited
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
if($request->isXMLHttpRequest()){
//After some code debuging, this is never
//executed
$month = $request->get('month');
$year = $request->get('year');
$sub_date = $month .'/' .$year;
}
$notesDeFrais = $form['ndf']->getData();
foreach ($notesDeFrais as $ndf) {
$ndf->setUser($user);
$ndf->setMonth($sub_date);
$em->persist($ndf);
}
$em->flush();
}
return $this->render('AvPlatformBundle:Platform:saisie.html.twig',
array(
'year' => $thisyear, 'form' => $form->createView()
));
}
And the script inside my saisie.html.twig view:
$(".month").click(function() {
var click = $(this);
var month = click.val();
var year = $("#years").val();
$.post("{{ path('avaliance_platform_saisie') }}",
{ 'month' : month,
'year' : year
},
function (data,status) {
alert('Data sent');
});
});
</div>
I don't really understand what is the server language here but I think in your post request you are missing. contentType (type of data you are sending to the server) and dataType (The type of data that you're expecting back from the server)
contentType:"application/json; charset=utf-8", dataType:"json",
Also, check whether your controller action method is configured to receive post http data.
From the source code:
/**
* Returns true if the request is a XMLHttpRequest.
*
* It works if your JavaScript library sets an X-Requested-With HTTP header.
* It is known to work with common JavaScript frameworks:
*
* @see http://en.wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript
*
* @return bool true if the request is an XMLHttpRequest, false otherwise
*/
public function isXmlHttpRequest()
{
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
}
Therefore, the problem is with the AJAX request not having this header. JQuery should put it in automatically, unless there is a reason to exclude it. A cross-domain request would be one such reason.
It is possible to manually insert the header (but only use this for debugging):
$.post("{{ path('avaliance_platform_saisie') }}",
{ 'month' : month,
'year' : year
},
headers: {'X-Requested-With': 'XMLHttpRequest'},
function (data,status) {
alert('Data sent');
});
});