【问题标题】:Can't get POST data because it's always null无法获取 POST 数据,因为它始终为空
【发布时间】:2019-09-20 07:32:42
【问题描述】:

当我尝试在 Symfony 2.8 中获取 POST 数据时,我不断收到null。我正在使用 Angular 服务进行这样的调用:

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

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

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

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

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

public function saveEmployeeWorkHoursAction(Request $request){
    $response = $request->request->all(); // Gives array(0)
    $response = json_encode($response);
    $response = new Response($response);
    return $response; // Returns []
}

【问题讨论】:

    标签: php angular symfony symfony-2.8


    【解决方案1】:

    您在请求正文中发送数据/有效负载,而不是作为单个参数。

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

    你需要做的:

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

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      相关资源
      最近更新 更多