SYmfony2请求无法获得post变量。

时间:2022-01-10 23:08:36

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...

我试图在symfony 2.4控制器中获取POST请求的变量。我通常对此没有异议,但对于这个……

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:

现在,当我查看控制台时,我可以看到post请求:

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"}"

第一个var转储为“{”维传递“:“ChartOfAccounts”

so there is something !

所以有一些事情!

but the second is only "NULL"

但第二项只是“空”

what am I doing wrong ?

我做错了什么?

2 个解决方案

#1


2  

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

显然,Angularjs $http post变量不会转到$_POST变量,因此不可能将它们放在$request->request->all()中。

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

但是它们看起来是“原始的”,因为您可以使用:$request->getContent();

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

简单地使用$myvar = json_decode($request->getContent())就可以做到这一点

#2


1  

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

我根本不知道angularjs,但它在主体中看起来像JSON,而不是请求主体参数。

Try:

试一试:

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

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

如果您发送post参数,您应该看到以下内容:

array 'dimensionpassed' => string 'ChartOfAccounts'

数组'dimensionpass ' =>字符串'ChartOfAccounts'

And the request body should look something like:

请求机构应该是这样的:

'dimensionpassed=ChartOfAccounts'

“dimensionpassed = ChartOfAccounts”

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

因此,您可能只需要让您的文章从Javascript正常工作(不好意思,这没有帮助)。

#1


2  

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

显然,Angularjs $http post变量不会转到$_POST变量,因此不可能将它们放在$request->request->all()中。

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

但是它们看起来是“原始的”,因为您可以使用:$request->getContent();

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

简单地使用$myvar = json_decode($request->getContent())就可以做到这一点

#2


1  

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

我根本不知道angularjs,但它在主体中看起来像JSON,而不是请求主体参数。

Try:

试一试:

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

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

如果您发送post参数,您应该看到以下内容:

array 'dimensionpassed' => string 'ChartOfAccounts'

数组'dimensionpass ' =>字符串'ChartOfAccounts'

And the request body should look something like:

请求机构应该是这样的:

'dimensionpassed=ChartOfAccounts'

“dimensionpassed = ChartOfAccounts”

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

因此,您可能只需要让您的文章从Javascript正常工作(不好意思,这没有帮助)。