简体   繁体   English

关于AJAX,symfony2-contollerAction

[英]regarding AJAX, symfony2 - contollerAction

i called a controllerAction in symfony2 from jquery script in twig template for form validation and verification. 我从树枝模板中的jquery脚本在symfony2中调用了controllerAction进行表单验证和验证。 After a successful response i called another controller from jquery script using $.post() for rendering a template for user-dashboard. 成功响应后,我使用$ .post()从jquery脚本中调用了另一个控制器,以呈现用户仪表板的模板。 but all i am getting is a template displayed in console but not in the user browser window. 但是我得到的只是一个模板显示在控制台中,而不显示在用户浏览器窗口中。 How to achieve this in the above stated way. 如何以上述方式实现这一目标。 ?? ??

Thanks for help (in advance) AKSHAT. 感谢您的帮助(提前)。

<script>
// my jquery code
$(document).ready(function()
{
$('.sign_in_box').click(function()
{
var data = new Object();
data.email=$('.email_txt').val();
data.password=$('.pwd_txt').val();
$.ajax('{{ path('login_login_validation') }}', {
    type : 'POST',
    data : data,
    success : function(response){
       var json = JSON.parse(response);
       var flag = json.success;
       if(flag)
           {
           $('#pwd_login_error').css('opacity' , '0');    
           $('#email_login_error').css('opacity' , '0' );
           $.ajax('{{ path('login_login_verification') }}',{
           type : 'POST',
           data : data,
           success : function(response){
               var login = JSON.parse(response);
               if(login.login)
                   {
                       alert("login success");
                       $.post('{{ path('login_dashboard') }}', data);
                   }
                   else
                       {
                        $('#pwd_login_error').css('opacity' , '0.5' );
                       }      
   }});
           }
       else
       {
            $('#pwd_login_error').css('opacity' , '0' );
           $('#email_login_error').css('opacity' , '0.5' );
        }  
        }
});   
});
});    
</script>


//////////////////////////////////////////////////////////////////////////////////////////////////////////
// controller
<?php

namespace Login\Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DashboardController extends Controller
{
public function indexAction()
{
    $email = $this->get('request')->get('email');
    return $this->render('LoginBundle:Dashboard:index.html.twig', array('email'=> $email));

}

}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// routing file

login_homepage:
pattern:  /
defaults: { _controller: LoginBundle:Homepage:index }


login_login_validation:
pattern:  /ajax_validate
defaults: { _controller:  LoginBundle:Login:validation }


login_login_verification:
pattern: /ajax_verify
defaults: { _controller: LoginBundle:Login:verification }

login_dashboard:
pattern: /dashboard
defaults: { _controller: LoginBundle:Dashboard:index }


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// twig file
{% block message %}
hello ! {{ email | json_encode | raw }}
welcome to dashboard .
{% endblock%}

The only wrong thing is this your are rendering the index page . 唯一的错误是您正在渲染索引页面。 after successfull login you have to redirect you page by 成功登录后,您必须重定向您的页面

window.location = "http://somewhereelse.com";

Or You can also append data in any div , Its on to you which way you want to follow . 或者,您也可以将数据附加到任何div中,以哪种方式跟踪它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM