Sonata Admin - 自定义 AJAX 调用 [英] Sonata Admin - Custom AJAX call

查看:26
本文介绍了Sonata Admin - 自定义 AJAX 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 sonata admin 中创建了一个自定义列表视图来显示日历.

I have created a custom list view in sonata admin to display a calendar.

我正在尝试动态地将事件添加到日历中,但我收到一个错误,CSRF 令牌无效.

I'm trying to add events to the calendar dynamically, but I'm getting an error with the CSRF token being invalid.

我有以下代码:

public function listAction()
{
    if (false === $this->admin->isGranted('LIST')) {
        throw new AccessDeniedException();
    }

    $datagrid = $this->admin->getDatagrid();
    $formView = $datagrid->getForm()->createView();

    // set the theme for the current Admin Form
    $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());

    $em = $this->getDoctrine()->getManager();
    $events = $em->getRepository('BMCrmBundle:Event')->findAll();

    $event = new Event();

    $formEvent = $this->createForm(new EventType(), $event );

    return $this->render($this->admin->getTemplate('list'), array(
        'action'     => 'list',
        'form'       => $formView,
        'datagrid'   => $datagrid,
        'csrf_token' => $this->getCsrfToken('sonata.batch'),
        'events'     => $events,
        'formEvent'  => $formEvent->createView()
    ));
}

查看

var url = "{{ path('create_event', { _sonata_admin: 'bm.crm.admin.event'} ) }}";
$.post(url, form.serialize(), function(data) {
   alert(data);
});

这总是返回CSRF令牌无效

This always returns that the CSRF token is invalid

有什么想法吗?

推荐答案

检查在您看来,您是否有以下行:

Check if in your view, you have the following line:

{{ form_rest(form) }}

因为我相信您正在逐个呈现表单字段,而不是一次呈现整个表单,并且忘记呈现表单的其余部分,其中包含 CSRF 令牌.

because I believe that you are rendering form fields one by one and not the whole form at once and forgot to render the rest of the form, which contains the CSRF token.

这篇关于Sonata Admin - 自定义 AJAX 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆