我有一个用Ajax调用的控制器。在这个控制器中,我有一个条件,比如
if($a == 10){
return $this->redirectToRoute('form_finish');
}
但是当条件满足时,我没有重定向
您在ajax中调用控制器,但您是否管理响应。
如果满足条件,则必须从浏览器重定向,或者处理ajax响应并将内容加载到DOM中。
我是这样解决的:
在控制器中,我把重定向处理程序放在json:
//return $this->redirectToRoute('fos_user_security_logout');
$response = new JsonResponse(array(
'route' => $this->get('router')->generate('fos_user_security_logout'),
'message' => 'You may choose on option'));
return $response;
在ajax中
success: function(response,xhr){
//This is for the redrection in front page
if(response.route) { window.location.href=response.route;}
希望对大家有所帮助