How to get Ajax post request by symfony2 Controller How to get Ajax post request by symfony2 Controller symfony symfony

How to get Ajax post request by symfony2 Controller


First, you don't need to access the container in your controller as it already implements ContainerAware

So basically your code should look like this in your Controller.php

public function ajaxAction(Request $request){    $data = $request->request->get('request');}

Also, make sure by the data you are sending is not null by using console.log(data) in the JS of your application.

And finally the answer of your question : you are not using the right variable, you need to access the value of $('#request').val() but you stored it in a request variable and you used a data variable name in your controller.

Consider changing the name of the variable, because it's confusing.


If you're sending the data as JSON — not as form urlencoded — you need to access the request body directly:

$data = json_decode($request->getContent());


You are doing it wrong when obtaining the value, you must use:

$data = $request->request->get('request');

'cause request is the name of your parameter.