I’m currently working on a symfony2 based project with lots of ajax usage.
Now I want to send a JSON via $.ajax(); (POST type) and handle it in a symfony2 controller.
But I’m not quite sure how I access the JSON inside the controller.
Right now I’ve got the following:
JS:
$.ajax({
url: url,
type:"POST",
data:json,
success:function (data) {
$('div.tooltip p').html(data);
}
});
And the PHP:
public function registrationAction(Request $request) {
if($request->getMethod() == 'POST') {
// How to receive??
}
return $this->render('KnowHowERegistrationBackendBundle:Create:registration.html.twig');
}
The only thing I don’t know is how can I access the JSON?
I’m sure it’s pretty easy I just don’t see it.
Thanks for your help!
your code i think not complete,if you want to send data to server with json format i think the setup $.ajax like this, just example
add parameter JSONFile or whatever what do you want.and you can use json decode to retrive json from client.
this is code in php:
in symfony2 controller direct acces
$_POSTis bad so use request$request = $this->getRequest();$request->request->get('JSONFile'); // get a $_POST parameter