0
votes

I'm trying to do an Ajax request from my angularJs controller to my Symfony controller. However, for an unknown reason, I cannot receive the data in my Symfony controller. My controller gets called and I can return some information that I will see in the success function on the AngularJS side. However, the data I'm sending via AngularJs cannot be retrieved on the Symfony controller.

Here's what I'm doing on the AngularJS side:

$http.post('{{ path('admin_ima_processmanagement_project_save', {'id':object.id}) }}',{"projectJson":"test"}).
                        success(function(data, status, headers, config) {
                            console.log("yeah");
                            console.log(data);
                        }).
                        error(function(data, status, headers, config) {
                            // called asynchronously if an error occurs
                            // or server returns response with an error status.
                            console.log("oh non");
                            console.log(data);
                        });

I can see in my console "yeah" that is appearing after the execution of this request.

In my Symfony controller, I have the following:

$request = $this->container->get('request');

$projectJson = $request->query->get('projectJson');

$response = array("code" => 100, "success" => true, "projectJson" => $projectJson);

return new Response(json_encode($response));

On the console, after the call, I get {"code":100,"success":true,"projectJson":{}} meaning that projectJson is unfortunately empty...

What should I do to retrieve the data that I'm sending from my client ?&

2
Your posted json is delivered as content. Use: $projectJson = json_decode($request->getContent(),true); print_r($projectionJson); die(); - Cerad

2 Answers

1
votes

In class Request property query refers to GET parameters.

In your case you need to access to POST parameters, which are in request property.

So your code should look like this:

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

More info about Request you will find here.

1
votes

Symfony2 does not support AngularJS $http data. Because AngularJS sends data as request body, and SF2 reads only $_GET and $_POST.

You have 2 solutions: