php - 交响乐团 3 : Call controller deleteAction with Ajax

标签 php ajax symfony

我正在尝试使用 ajax 删除 Symfony3 实体。

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

Controller

/**
 * @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()
    ;
}

Twig

$.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 token 。快速修复是将 token 添加为数据:

$.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);
    }
});

form 这里是表单名称,也可以手动设置。例如。 删除表单:

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()
    ;
}

与:

$.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);
    }
});

关于php - 交响乐团 3 : Call controller deleteAction with Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47157814/

相关文章:

php - .htaccess 域根目录之外的错误页面

php - Laravel:选择同一天创建的条目

javascript - 使用什么事件来在选择文本框中的值时显示警报消息

javascript - Ajax帮助加载内容

Javascript不会访问json对象

php - 从 MySql 查询打印更多结果

php - Symfony 4 全局路由前缀

Symfony Assetic 功能似乎不起作用

javascript - 使用 TinyMCE 和 Symfony3 实现 FileMangerBundle (artgris)

php - 如何在 phpunit 中模拟 git diff 的结果