2

I'm trying to get the variable of a POST request in a symfony 2.4 controller. I usually have no issues on that but for this one...

this is how I call the controller:

var deferred = $q.defer();
     $http({method:'POST',data:{dimensionpassed:dimension},url:Routing.generate('_API_getTree')}).success(function(result){
         deferred.resolve(result); 
      });                

return deferred.promise;

Now when I look in the console I can see the post request:

JSON
dimensionpassed
    "ChartOfAccounts"
Source
{"dimensionpassed":"ChartOfAccounts"}

And here is the controller:

public function _API_getTreeAction(Request $request)
{
$test = $request->getContent();
var_dump($test);
$test =  $request->request->get('dimensionpassed');
var_dump($test);
}

The first var dump gives me "{"dimensionpassed":"ChartOfAccounts"}"

so there is something !

but the second is only "NULL"

what am I doing wrong ?

2 Answers 2

3

Apparently, Angularjs $http post variable don't go to the $_POST variable, hence it's impossible to have them in the $request->request->all().

However they appear "raw" as you can have them with this:$request->getContent();

Simply using $myvar = json_decode($request->getContent()) does the trick

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

1 Comment

even when we post via api platform !
1

I don't know angularjs at all, but that looks like JSON in the body, not request body parameters.

Try:

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

If you're sending post parameters you should see something like:

array 'dimensionpassed' => string 'ChartOfAccounts'

And the request body should look something like:

'dimensionpassed=ChartOfAccounts'

So you probably just need to get your POST from Javascript working properly (can't help there sorry).

1 Comment

it's empty when I do this... let's look angular side then

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.