Php Symfony2:CSRF令牌无效。请尝试重新提交表单
我有一张表格,需要在字段中填写一些信息。 即使我在里面放了东西,我也会得到错误:Php Symfony2:CSRF令牌无效。请尝试重新提交表单,php,symfony,csrf,Php,Symfony,Csrf,我有一张表格,需要在字段中填写一些信息。 即使我在里面放了东西,我也会得到错误: The CSRF token is invalid. Please try to resubmit the form 与此问题相关:我正确地使用了$form->bindRequest() 以下是我的模板(细枝)代码: 但我仍然收到相同的错误。使用以下命令发送序列化表单: 通过这种方式,Symfony将像处理普通请求一样处理提交请求——您不必做任何特殊的事情来处理Ajax表单提交。你需要做的就是返回一个-当然,如果
The CSRF token is invalid. Please try to resubmit the form
与此问题相关:我正确地使用了$form->bindRequest()
以下是我的模板(细枝)代码:
但我仍然收到相同的错误。使用以下命令发送序列化表单: 通过这种方式,Symfony将像处理普通请求一样处理提交请求——您不必做任何特殊的事情来处理Ajax表单提交。你需要做的就是返回一个-当然,如果你需要的话 以下是处理表单的示例-根据您的需要调整表单:
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
// do something here — like persisting or updating an entity
return new JsonResponse([
'success' => true,
]);
}
return new JsonResponse([
'success' => false,
'form' => $this->render($pathToTheTemplateHere, [
'form' => $form,
],
]);
}
另一种方法是使用不同的模板:
form.json.twig
和form.html.twig
-阅读文档了解详细信息。您也可以粘贴模板吗?@KristianZondervan,是的,我会尽快编辑我的问题。@KristianZondervan,完成。也许我忘了提到我在表单中也使用了Ajax。表单在没有使用Ajax的情况下工作吗?@elnur,刚刚添加了js代码。谢谢@elnur,但是数据是什么?我怎样才能在这里得到数据?这是一个数组?谢谢。在Symfony方面,您处理表单的方式与任何普通表单一样。好的。请再问一个问题,如何返回JsonResponse?来自“函数(响应)”的“响应”是jsonResponse?我现在有另一个与此auestion相关的问题。我之前尝试做的是,一旦成功(使用ajax),我将把返回的数据转换为html元素。现在,我得到了一个json响应,我想我不知道如何解析我需要的daat。谢谢,这里是:
<div class="item item-last">
<h1>Create Affiliation</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.affiliation) }}
{{ error }}
</div>
{% endif %}
{% if app.session.flash('user-notice') != '' %}
<div class="flash-notice">
{% autoescape false %}
{{ app.session.flash('user-notice') }}
{% endautoescape %}
</div>
{% endif %}
</div>
<div class="item item-last">
<form action="{{ path('SciForumVersion2Bundle_user_submission_affiliation_create', {'hash_key' : submission.hashkey, 'author_id' : author.id }) }}?ajax=no" method="POST" class="authorForm" {{ form_enctype(form) }}>
<div style="float:left;">
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
{{ form_label(form.affiliation) }}
</td>
<td>
{{ form_widget(form.affiliation, { 'attr': {'size': 40} }) }}
</td>
</tr>
<tr>
<td>
</td>
<td>
<div class="button button-left button-cancel">
<img src="{{ asset('bundles/sciforumversion2/images/design/new/button-red.png') }}"/>
<a href="{{ path('SciForumVersion2Bundle_user_submission_author_edit', { 'hash_key' : submission.hashkey, 'author_id' : 0 }) }}" class="submission_link">cancel</a>
</div>
<div style="float: left;"> </div>
<div class="button button-left button-cancel">
<img src="{{ asset('bundles/sciforumversion2/images/design/new/button.png') }}"/>
<input type="submit" name="login" value="submit" />
</div>
<div style="clear: both;"></div>
</td>
</tr>
</table>
</div>
{{ form_rest(form) }}
</form>
</div>
function init_submission_functions()
{
init_fck();
$(".submission_link").unbind("click").bind("click", function() {
var href = $(this).attr("href");
if( href == null || href == '' ) return false;
$.ajax({
type: "POST",
async: true,
url: href,
cache: false,
dataType: "json",
success: function(data) {
$("#content .contentwrap .itemwrap").html( data.content );
init_submission_functions();
}
});
return false;
});
$(".authorForm").unbind("submit").bind("submit", function() {
var href = $(this).attr("action");
if( href == null || href == '' ) return false;
var affiliation = "blabla";
$.ajax({
type: "POST",
async: true,
url: href,
affiliation: affiliation,
cache: false,
dataType: "json",
success: function(data) {
$("#content .contentwrap .itemwrap").html( data.content );
init_submission_functions();
}
});
return false;
});
}
$form.submit(function (e) {
e.preventDefault();
$this = $(this);
$.post($this.attr('action'), $this.serialize(), function (response) {
// handle the response here
});
});
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
// do something here — like persisting or updating an entity
return new JsonResponse([
'success' => true,
]);
}
return new JsonResponse([
'success' => false,
'form' => $this->render($pathToTheTemplateHere, [
'form' => $form,
],
]);
}