无法获取POST数据,因为它始终为null

时间:2019-09-20 07:32:42

标签: php angular symfony symfony-2.8

当我尝试在Symfony 2.8中获取POST数据时,我一直在获取null。我正在使用Angular服务进行这样的呼叫:

this.http.post(`****/app/relation/saveEmployeeWorkHours`, JSON.stringify({workHours}));

如下所示,我正在发送的数据的有效负载是可见的:

enter image description here

在我的路线上,我有这个:

app_addEmployeeDays:
  path: /app/relation/saveEmployeeWorkHours.{_format}
  defaults: { _controller: CompoRelationBundle:Api\Relation:saveEmployeeWorkHours, _format: 'json' }
  requirements:
    _method : POST

在$ request的操作中,我得到null的{​​{1}},但确实得到了URL中发送的get参数(用于调试):

POST

1 个答案:

答案 0 :(得分:2)

您是在请求的正文中发送数据/有效载荷,而不是作为单独的参数发送

您需要访问。 request->all()检索查询和发布参数,而不是正文。

您需要这样做:

$body     = $request->getContent();
$response = json_encode($body);
// etc