Php Symfony 3:使用Ajax调用控制器deleteAction

Php Symfony 3:使用Ajax调用控制器deleteAction,php,ajax,symfony,Php,Ajax,Symfony,我试图用ajax删除一个Symfony3实体 问题是$form->isValid()返回false,但表单(或任何子元素)上没有错误。我错过了什么 控制器 /** * @Route("/{profileID}/delete", name="profile_delete") * @ParamConverter("Profile", options={"mapping": {"profileID": "id"}}) * @Method("DELETE") */ public function

我试图用ajax删除一个Symfony3实体

问题是
$form->isValid()
返回false,但表单(或任何子元素)上没有错误。我错过了什么

控制器

/**
 * @Route("/{profileID}/delete", name="profile_delete")
 * @ParamConverter("Profile", options={"mapping": {"profileID": "id"}})
 * @Method("DELETE")
 */
public function deleteAction(Request $request, Profile $Profile)
{
    $form = $this->createDeleteForm($Profile);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->remove($Profile);
        $em->flush();

        return new JsonResponse(array('message' => 'profile removed'));
    } else {
        return new JsonResponse(array('message' => 'error'));
    }
}

private function createDeleteForm(Profile $Profile)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('profile_delete', array('profileID' => $Profile->getId())))
        ->setMethod('DELETE')
        ->getForm()
    ;
}
小树枝

$.ajax({
  url: "{{ path('profile_delete', {'profileID': Profile.id}) }}",
  type: 'delete',
  success:function(data){
    console.log(data);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    console.log(textStatus);
    console.log(errorThrown);
  }
});

您提交的表单没有csrf令牌。快速修复方法是将令牌添加为数据:

$.ajax({
    url: "{{ path('profile_delete', {'profileID': Profile.id}) }}",
    type: 'delete',
    data: {
        form: {
            _token: "{{ csrf_token('form') }}"
        }
    },
    success:function(data){
        console.log(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log(textStatus);
        console.log(errorThrown);
    }
});
表单
这是表单名称,也可以手动设置。例如,
删除表单

private function createDeleteForm(Profile $Profile)
{
    return $this
        ->get('form.factory')
        ->createNamedBuilder('delete-form', FormType::class, null, [
            'action' => $this->generateUrl('profile_delete', array('profileID' => $Profile->getId())),
            'method' => 'DELETE',
        ])
        ->getForm()
    ;
}
与:


您在探查器中没有表单/日志问题?我猜csrf令牌丢失,但是必需的。@转到否,探查器没有报告任何问题@Yoshi令牌应如何提交?我尝试过将整个表单数据序列化,但也使用
数据:{u-token:value}
发送,得到了相同的结果,谢谢!事实上,表单名称必须与令牌一起出现在请求中,您没有表单错误不是很奇怪吗?我记得令牌将其错误绑定到表单parent@goto不完全是。如果post数据中没有表单名称,则该表单未正式提交。因此,尚未进行任何错误检查。假设我们提交
数据:{form:{}}
,那么令牌错误就会出现。
$.ajax({
    url: "{{ path('profile_delete', {'profileID': Profile.id}) }}",
    type: 'delete',
    data: {
        'delete-form': {
            _token: "{{ csrf_token('delete-form') }}"
        }
    },
    success:function(data){
        console.log(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log(textStatus);
        console.log(errorThrown);
    }
});