您能解释一下为什么Axios请求在我的Symfony应用程序中不起作用吗?
我将Axios请求与React一起使用:
handleSubmit = () => {
axios.put('/families/' + this.props.familyId + '/edit',{
parents: "test"
})
.then(response => {
alert('Family has been modified');
})
};
我的控制器:
/**
* Edit Family
*
* @Route("families/{id}/edit", name="family_edit")
* @Method(methods={"PUT"})
* @param Request $request
* @param $id
*/
public function editFamilyAction(Request $request, $id)
{
$parents = $request->get('parents');
...
}
但是$ parents等于null ...
发生了什么,我需要在某处进行一些配置吗?
请提供帮助!
丹尼斯
答案 0 :(得分:0)
就这么简单:
/**
* @Route("/families/{id}/edit", name="family_edit", methods={"PUT"})
* @param Request $request
* @param $id
*/
public function editFamilyAction(Request $request, $id)
{
$data = $request->getContent();
$data = json_decode($data);
$parents = $data->parents;
// do your stuff
}
请注意,如果您正在使用Symfony 4,则不建议使用方法注释,而应像上面的代码中那样正确配置Route注释。
答案 1 :(得分:0)
哇,它似乎正在工作!非常感谢你!!
你能解释一下我的错误吗?
我刚刚复制/粘贴了团队代码,但没有成功。让我解释一下代码:
反应:
handleSaveDiagnostic = () => {
axios.put('/admin/api/diagnostic/update-diagnostic/'+ this.state.currentDiagnostic.id, {
'newDiagnostic': this.state.currentDiagnostic
})
.then(response => {
alert('Family has been modified');
this.loadCounters();
})
};
控制器:
* @Route("/update-diagnostic/{diagnostic}", name="update_diagnostic")
* @Method(methods={"PUT"})
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \AppBundle\Entity\Diagnostic $diagnostic
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function updateDiagnosticAction(Request $request, Diagnostic $diagnostic) {
$newDiagnostic = $request->get('newDiagnostic'); (is working!)
...
}
请问有什么区别,它对他有用,但是我需要$ request-> getContent吗?!!