Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I receive a JSON in symfony2

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!

like image 600
Johannes Klauß Avatar asked Mar 15 '12 10:03

Johannes Klauß


People also ask

How do you get the data from a JSON response?

GET JSON dataawait fetch('/api/names') starts a GET request, and evaluates to the response object when the request is complete. Then, from the server response, you can parse the JSON into a plain JavaScript object using await response. json() (note: response.

How do I request a JSON response to API?

To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON.

Can we send JSON in GET request?

To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.


2 Answers

in your ajax request u must set the content type to application/json:

$.ajax({
      url: url,
      type:"POST",
      contentType: 'application/json',
      data:json,
      success:function (data) {
           $('div.tooltip p').html(data);
      }
});

and in your controller use this to parse the content:

if($request->getMethod() == 'POST') {
    if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
        $data = json_decode($request->getContent(), true);
        $request->replace(is_array($data) ? $data : array());
    }
}
like image 50
Lhassan Baazzi Avatar answered Sep 16 '22 23:09

Lhassan Baazzi


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

$.ajax({
                url: url,
                type:"POST",
                data:"JSONFile=" + json,
                success:function (data) {
                    $('div.tooltip p').html(data);
                }
            });

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:

$json = $_POST['JSONFile'];

var_dump(json_decode($json));
var_dump(json_decode($json, true)); //true option if you will convert to array

in symfony2 controller direct acces $_POST is bad so use request $request = $this->getRequest(); $request->request->get('JSONFile'); // get a $_POST parameter

like image 45
viyancs Avatar answered Sep 20 '22 23:09

viyancs