当我第二次在服务器上发送带有数据的表单时,它将在处理来自客户端请求的路由上重定向我。
我的意思是说
我有控制器方法,可以接受来自客户端的请求作为AJAX并验证数据
public function addCommentAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostType(), $post);
$post->setCreated(new \DateTime('now'));
if ($request->getMethod() == 'POST') {
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
$this->get('session')->getFlashBag()->add('success', 'Comment has been sent on moderation. Wait 1 minute before send another comment.');
}
}
return $this->render('GuestbookBundle:Post:postForm.html.twig', array(
'form' => $form->createView(),
));
}
我的阿贾克斯序列化表单,然后使用表单中的数据将请求发送到服务器
$('.container .form-comment .send-comment').on('click', function(e) {
var $from = $(this).closest('form');
e.preventDefault();
$.ajax({
type: "POST",
url: $from.attr('action'),
data: $from.serialize(),
success: function(data) {
$('.form-comment').empty().append(data);
}
})
});
当我第二次单击发送按钮时,它将我重定向到页面mydomain.com/add,该页面处理来自表单的请求
add:
path: /add
defaults: { _controller: GuestbookBundle:Post:addComment }
requirements:
_method: POST
如何解决这个问题?为什么成功后发送我的表格不清楚?
谢谢。
参考方案
首先,您提交后又要返回表格,为什么需要这个?
如我所见,您正在尝试添加评论,因此提交后,您可以返回消息(例如-成功)或您需要的任何其他数据。
public function addCommentAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostType(), $post);
$result = ['status' => 'success',
'comment' => 'Comment has been sent on moderation. Wait 1 minute before send another comment.' ];
$post->setCreated(new \DateTime('now'));
if ($request->getMethod() === 'POST') {
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
// $this->get('session')->getFlashBag()->add('success', 'Comment has been sent on moderation. Wait 1 minute before send another comment.');
return new JsonResponse($result);
}
$result['status'] = 'error';
return new JsonResponse($result);
}
return $this->render('GuestbookBundle:Post:postForm.html.twig', array(
'form' => $form->createView(),
));
}
javascript:
$('.container .form-comment .send-comment').on('click', function(e) {
var $from = $(this).closest('form');
e.preventDefault();
$.ajax({
type: "POST",
url: $from.attr('action'),
data: $from.serialize()
}).done(function(data) {
//data will contains data.status && data.message
//you can add any data in your controller
$('.form-comment').empty().append(data.messages);
}).fail(function(data) {
alert(data.messages);
});
});
您也使用了不赞成使用的语法,
最好使用$form->handleRequest($request);
,您可以阅读here
请如何排序以下数组Array ( 'ben' => 1.0, 'ken' => 2.0, 'sam' => 1.5 ) 至Array ( 'ken' => 2.0, 'sam' => 1.5, 'ben' =&…
PHP PDO组按列名称查询结果 - php以下PDO查询返回以下结果:$db = new PDO('....'); $sth = $db->prepare('SELECT ...'); 结果如下: name curso ABC stack CDE stack FGH stack IJK stack LMN overflow OPQ overflow RS…
PHP strtotime困境 - php有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00…
PHP-全局变量的性能和内存问题 - php假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…
php-casperjs获取内部文本 - php我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…