我在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);
}当我将变量($piece、$type、$startDate、$endDate)替换为字符串时,post请求可以正常工作。但当我尝试通过Postman或javascript这样的东西发出post请求时:
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错误!我不明白--谢谢你的帮助!
发布于 2017-12-26 20:20:11
查看您的日志,您的错误是什么。
但是你必须改进你的控制器!您没有使用Symfony表单,也没有任何验证。即使你现在没有500错误,如果你不尊重你的实体类型,你将来也会有一些错误。
我也不明白为什么要使用post请求,而不是在主体中传递所有参数。您只能在url中使用get参数。
发布于 2017-12-28 04:44:35
在不知道是什么例外的情况下,你什么都不知道:
尝试查看响应正文
没有这些信息,我只有一个想法。
如果你正在开发环境中工作,那么你的js请求可能根本看不到路由(和其他东西)。
清除缓存文件夹的所有内容
rm -rf var/cache/*
和
php bin/console c:w -e prod
https://stackoverflow.com/questions/47978423
复制相似问题