如何让jQuery返回csrf? [英] How to have jquery to return csrf?

查看:51
本文介绍了如何让jQuery返回csrf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读此 Symfony CSRF和Ajax 只会返回csrf,但是应该如何jQuery将其返回到表单吗?

Reading this Symfony CSRF and Ajax it just returns the csrf, but how should be the jQuery to return it to form?

我的代码如下:

$formFactory = Forms::createFormFactoryBuilder()
    ->addExtension(new CsrfExtension($csrfManager))
    ->getFormFactory();


$defaultFormTheme = 'bootstrap_3_layout.html.twig';

$vendorDir = realpath(__DIR__.'/../vendor');
$appVariableReflection = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
$vendorTwigBridgeDir = dirname($appVariableReflection->getFileName());
$viewsDir = realpath('twig');

$twig = new Twig_Environment(new Twig_Loader_Filesystem(array(
    $viewsDir,
    $vendorTwigBridgeDir.'/Resources/views/Form',
)));
$formEngine = new TwigRendererEngine(array($defaultFormTheme), $twig);
$twig->addRuntimeLoader(new \Twig_FactoryRuntimeLoader(array(
    TwigRenderer::class => function () use ($formEngine, $csrfManager) {
        return new TwigRenderer($formEngine, $csrfManager);
    },
)));
$twig->addExtension(new FormExtension());

$translator = new Translator('en');
$twig->addExtension(new TranslationExtension($translator));
$form = $formFactory->createBuilder()
    ->add('task', TextType::class)
    ->add('dueDate', DateType::class)
    ->getForm();

$request = Request::createFromGlobals();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    $data = $form->getData();
    print_r($data);
}

$twig->display('new.html.twig', array(
    'form' => $form->createView(),
));

然后在我的表单模板中:

Then in my form template I have:

<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script>
$.ajax({
    url: "app.php",
    type: 'POST',
    data: {
        form: {
            _token: "{{ csrf_token('form') }}"
        }
    },
});
</script>
</head>
<body>

{{ form_start(form, {'method': 'POST', 'action': 'app.php'}) }}
{{ form_row(form.task, {'required': false}) }}
    <input type="submit" />
{{ form_end(form, {'render_rest': false}) }}

当我查看源代码时,我会看到:

When I look at my source code I see:

<script>
$.ajax({
    url: "app.php",
    type: 'POST',
    data: {
        form: {
            _token: "Kf1IK4uikxPCxfmjUkDa8vhRiNI4PWS2zdnTIiWyHC4"
        }
    },
});
</script>
<form name="form" method="post" action="app.php">
<div class="form-group"><label class="control-label" for="form_task">Task</label><input type="text" id="form_task" name="form[task]" class="form-control" value="qqq" /></div>


    <input type="submit" />
</form>

因此生成了令牌,但是当我执行打印调试时,看不到_token被发布.我做错了什么?

So token is generated but when I do print debug, I don't see _token is posted. What wrong I did?

推荐答案

您可以按照链接答案之一中的描述序列化表格,或仅发送令牌

You can serialize the form as describe in one of the answer of your link, or only send the token

仅令牌:

$.ajax({
    url: "{{ path('your_edit_route') }}",
    type: 'POST',
    data: {
        form: {
            _token: "{{ csrf_token('form') }}"
        }
    },
});

完整形式:

var data = $('#targetForm').serialize();

$.ajax({
    url: "{{ path('your_edit_route') }}",
    type: 'POST',
    data: data,
});

这篇关于如何让jQuery返回csrf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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