ajax post request handle data in symfony2 controller ajax post request handle data in symfony2 controller symfony symfony

ajax post request handle data in symfony2 controller


Do you use the request object in your controller?

<?phpnamespace Acme\DemoBundle\Controller;use Symfony\Component\HttpFoundation\Request;//...other things to useclass MyController extends Controller{    public function handleRequestAction() {        $request = $this->get('request');        //request your data        $title   = $request->get('title');        //or in one line        $title   = $this->get('request')->request->get('title');    }}?>

This is my normal way when I want to get data from an ajax call.Could you post what $content contains?

I see no problem with posting the data like you did. Constructing a json object might be helpful but the way you're doing it seems fine to me. I did this too.

EDIT

Normally you could also access all data in the request by doing this:

$all = $request->request->all();

Maybe you could then var_dump() the variables to see if something is in them.


You can construct your json object and pass the JSON object to your controller using JSON.stringify.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

var obj = {      title: title,                      description: description,      questions: questions              };$.ajax({ type: "POST", url: Routing.generate('save'), contentType: 'application/json; charset=UTF-8', data: JSON.stringify(obj)});


quiz - form name serialize -populate the variables

 $.ajax({            url: $("#quiz").attr("action"),            data: $("#quiz").serialize(),            type: 'POST' });

or

$.ajax({                url: $("#commentForm").attr("action"),                data: {                    comment: commentFormID.val()                },                type: 'POST'});

Controller - More like what previous comments suggested.

$request = $this->get('request');$usercomment=$request->request->get('parameterName');