0

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 ?&

1
  • Your posted json is delivered as content. Use: $projectJson = json_decode($request->getContent(),true); print_r($projectionJson); die(); Commented Feb 9, 2014 at 23:59

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How can I target Symfony2 controller action from my Angular controller? I made my frontend including routing and templating in Angular.
1

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.