Php 与Symfony 3进行post请求?

Php 与Symfony 3进行post请求?,php,symfony,postman,Php,Symfony,Postman,我在Symfony3中有一个函数,可以将数据发布到mysql数据库。函数如下所示: /** * @Route("/LegoPieces") * @METHOD("POST") * @View() * * @Annotations\QueryParam( * name="piece", nullable=false, description="piece" * ) * @Annotations\QueryParam( * name="type", nullable=false,

我在Symfony3中有一个函数,可以将数据发布到mysql数据库。函数如下所示:

/**
 * @Route("/LegoPieces")
 * @METHOD("POST")
 * @View()
 *
 * @Annotations\QueryParam(
 * name="piece", nullable=false, description="piece"
 * )
 * @Annotations\QueryParam(
 * name="type", nullable=false, description="type"
 * )
 * @Annotations\QueryParam(
 * name="startDate", nullable=false, description="Start Date YYYY-MM-DD HH:MM:SS (Should be in the future)"
 * )
 * @Annotations\QueryParam(
 * name="endDate", nullable=false, description="End Date YYYY-MM-DD HH:MM:SS (Should be no more than 3 weeks in the future)"
 * )
 */
public function postAction(ParamFetcherInterface $paramFetcher)
{
    $piece = $paramFetcher->get('piece');
    $type = $paramFetcher->get('type');
    $start = $paramFetcher->get('startDate');
    $end   = $paramFetcher->get('endDate');
    $startDate = DateTime::createFromFormat('Y-m-d H:i:s', $start);
    $endDate = DateTime::createFromFormat(' Y-m-d H:i:s', $end);
    $em = $this->getDoctrine()->getManager('default');
    $data = new LegoPieces();
    $data->setPiece($piece);
    $data->setType($type);
    $data->setStartDate($startDate);
    $data->setEndDate($endDate);
    $em->persist($data);
    $em->flush();

    return new JsonResponse("LegoPieces Added Successfully", 200);
}
    fetch("http://localhost:8000/LegoPieces", {
    method: "POST",
    body: {
      startDate: this.startDate,
      endDate: this.endDate,
      piece: this.piece,
      type: this.type
    }
  }).then(response => response.json())
);
});
当我用字符串替换变量($piece、$type、$startDate、$endDate)时,post请求工作。但当我试图通过类似Postman或javascript这样的东西发出post请求时:

/**
 * @Route("/LegoPieces")
 * @METHOD("POST")
 * @View()
 *
 * @Annotations\QueryParam(
 * name="piece", nullable=false, description="piece"
 * )
 * @Annotations\QueryParam(
 * name="type", nullable=false, description="type"
 * )
 * @Annotations\QueryParam(
 * name="startDate", nullable=false, description="Start Date YYYY-MM-DD HH:MM:SS (Should be in the future)"
 * )
 * @Annotations\QueryParam(
 * name="endDate", nullable=false, description="End Date YYYY-MM-DD HH:MM:SS (Should be no more than 3 weeks in the future)"
 * )
 */
public function postAction(ParamFetcherInterface $paramFetcher)
{
    $piece = $paramFetcher->get('piece');
    $type = $paramFetcher->get('type');
    $start = $paramFetcher->get('startDate');
    $end   = $paramFetcher->get('endDate');
    $startDate = DateTime::createFromFormat('Y-m-d H:i:s', $start);
    $endDate = DateTime::createFromFormat(' Y-m-d H:i:s', $end);
    $em = $this->getDoctrine()->getManager('default');
    $data = new LegoPieces();
    $data->setPiece($piece);
    $data->setType($type);
    $data->setStartDate($startDate);
    $data->setEndDate($endDate);
    $em->persist($data);
    $em->flush();

    return new JsonResponse("LegoPieces Added Successfully", 200);
}
    fetch("http://localhost:8000/LegoPieces", {
    method: "POST",
    body: {
      startDate: this.startDate,
      endDate: this.endDate,
      piece: this.piece,
      type: this.type
    }
  }).then(response => response.json())
);
});

我犯了500个错误!我不明白-谢谢你的帮助

在日志中查找您的错误

但是你必须改进你的控制器!您不使用Symfony表单,也没有任何验证。即使您现在没有500个错误,如果您不尊重您的实体类型,将来也会有一些错误


我也不明白你为什么使用post请求,不在身体中传递所有参数。您只在url中使用get参数。

而不知道是什么异常导致了500-您什么都不知道:(

试着看看反应体

没有这些信息,我只有一个想法

如果您正在开发环境中工作,那么您的js请求可能根本看不到路由(以及其他内容)

清除缓存文件夹的所有内容

rm-rf var/cache/*


php-bin/console c:w-e prod

您是否检查了symfony profiler结果?我最初在javascript中有post请求,其主体用于传递参数(我编辑了post),但它没有显示任何内容。错误为“调用成员函数格式()在boolean上,我认为它显示了这一点,因为它没有获取日期参数!@Nespony是的,您应该使用表单组件来管理实体的水合作用并验证它们:),这样您就不必担心获取参数、日期格式和创建。在验证之后,您可以假设(使用良好的断言)您的实体可以持久化到db