你能解释一下为什么 Axios 请求在我的 Symfony 应用程序中不起作用吗?
我像这样将 Axios 请求与 React 结合使用:
handleSubmit = () => {
axios.put('/families/' + this.props.familyId + '/edit',{
parents: "test"
})
.then(response => {
alert('Family has been modified');
})
};
我的 Controller :
/**
* 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...
发生了什么,我需要在某处进行一些配置吗?
需要帮助!
丹尼斯
最佳答案
就这么简单:
/**
* @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,方法注释已被弃用,您应该像我在上面的代码中所做的那样正确配置路由注释。
关于ajax - Axios 放置请求和 symfony Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53343604/