我在Symfony2項目中有一個表單,我需要提交表單。第一個想法是使用ajax提交,但問題是這不會通過symfony2所需的驗證,並且我得到CSRF錯誤消息(請參閱我的前置問題:Symfony2: The CSRF token is invalid. Please try to resubmit the form)。Symfony2和提交機智JavaScript:如何獲取html內容
感謝@ Elnur的回答,我現在可以使用$ post提交表單,但有另一個問題。
隨着阿賈伊,我正在回一個HTML響應,我能ASIGN這個響應HTML元素:
$.ajax({
type: "POST",
async: true,
url: href,
cache: false,
dataType: "json",
success: function(data) {
$("#content .contentwrap .itemwrap").html(data.content);
}
});
這裏是我從得到響應:
<div class="item item-last">
<h1>Create Affiliation</h1>
<div class="error">
<ul><li>The CSRF token is invalid. Please try to resubmit the form</li></ul>
<ul><li>The Affiliation should not be blank</li></ul>
</div>
</div>
<div class="item item-last">
<form action="/app_dev.php/user/submission/affiliation/create/4a0ad9f8020c5cd5712ff4c4c8921b32?ajax=no" method="POST" class="authorForm" >
<div style="float:left;">
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
<label for="submissionAffiliationForm_affiliation" class=" required">Affiliation</label>
</td>
<td>
<input type="text" id="submissionAffiliationForm_affiliation" name="submissionAffiliationForm[affiliation]" required="required" size="40" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<div class="button button-left button-cancel">
<img src="/bundles/sciforumversion2/images/design/new/button-red.png"/>
<a href="/app_dev.php/user/submission/author/edit/4a0ad9f8020c5cd5712ff4c4c8921b32/0" class="submission_link">cancel</a>
</div>
<div style="float: left;"> </div>
<div class="button button-left button-cancel">
<img src="/bundles/sciforumversion2/images/design/new/button.png"/>
<input type="submit" name="login" value="submit" />
</div>
<div style="clear: both;"></div>
</td>
</tr>
</table>
</div>
<input type="hidden" id="submissionAffiliationForm__token" name="submissionAffiliationForm[_token]" value="de9690f61f0ee5f30fdcc5152f44e76787f34bbb" />
</form>
</div>
但現在,當使用後:
$.post($this.attr('action'), $this.serialize(), function (data) {
$("#content .contentwrap .itemwrap").html(data);
});
我沒有得到了HTML,但JSON格式的響應我不知道如何從中提取他的正確信息。
這裏是我從後得到響應:
{"responseCode":400,"errors":false,"submitted":false,"content":"<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\n <meta http-equiv=\"refresh\" content=\"1;url=\/app_dev.php\/user\/submission\/4a0ad9f8020c5cd5712ff4c4c8921b32\" \/>\n\n <title>Redirecting to \/app_dev.php\/user\/submission\/4a0ad9f8020c5cd5712ff4c4c8921b32<\/title>\n <\/head>\n <body>\n Redirecting to <a href=\"\/app_dev.php\/user\/submission\/4a0ad9f8020c5cd5712ff4c4c8921b32\">\/app_dev.php\/user\/submission\/4a0ad9f8020c5cd5712ff4c4c8921b32<\/a>.\n <\/body>\n<\/html>","notice":""}
這裏是如何看起來像個控制器:
$em = $this->getDoctrine()->getEntityManager();
// get the user object
$user = $this->get('security.context')->getToken()->getUser();
$submission = $em->getRepository('SciForumVersion2Bundle:Submission')->findOneBy(array("hash_key"=>$hash_key));
$author = $em->getRepository('SciForumVersion2Bundle:SubmissionAuthor')->findOneBy(array("id"=>$author_id, "hash_key"=>$hash_key));
if($author == null) {
$author = new SubmissionAuthor();
$author->setPerson(new Person());
}
$enquiry = new Affiliation();
$formType = new SubmissionAffiliationFormType();
$form = $this->createForm($formType, $enquiry);
$request = $this->getRequest();
$valid = true;
$error = '';
if($request->get('cancel') == 'yes') return $this->redirect($this->generateUrl('SciForumVersion2Bundle_user_submission', array("key"=>$submission->getHashKey())));
if ($request->getMethod() == 'POST' && $request->get('ajax') == 'no') {
$form->bindRequest($request);
if ($valid = $form->isValid()) {
if($valid) {
$em->persist($enquiry);
$em->flush();
$uAff = new UserAffiliation();
$uAff->setUserId($user->getId());
$uAff->setAffiliationId($enquiry->getId());
$uAff->setUser($user);
$uAff->setAffiliation($enquiry);
$em->persist($uAff);
$em->flush();
// Redirect - This is important to prevent users re-posting
// the form if they refresh the page
return $this->redirect($this->generateUrl('SciForumVersion2Bundle_user_submission', array("key"=>$submission->getHashKey())));
}
}
,你能告訴我們你的控制器? – Touki
@Touki,謝謝。我會在一分鐘內更新我的問題。 –
您的AJAX請求預計會有回報。 Symfony正試圖重定向用戶。您不能從AJAX請求重定向。如果你想顯示的內容只是做。 '$(「#content.contentwrap .itemwrap」)。html(data.content);' – Touki