symfony2.4 ajax call update or set session issues

Posted by user3797283 on Stack Overflow See other posts from Stack Overflow or by user3797283
Published on 2014-08-20T10:16:00Z Indexed on 2014/08/20 10:19 UTC
Read the original article Hit count: 207

Filed under:
|
|
|
|

i have an issue with symfony2 when i use ajax to set session, hope u pro guys can help me.

here is my controller code:

//show month event list
public function indexAction() {
    if ($this->getRequest()->isXmlHttpRequest()) {
        $paging = $this->getRequest()->get("nom");
        $session = $this->getRequest()->getSession();

        if ($paging) {
            //if $paging is set, then that's a click pager ajax event
            //(not 1st time load)
            $year = $paging;
            $session->set('year', $year);
        } else {
            //$paging is null, it's the first time page load
            $year = (new \DateTime())->format("Y");
            $session->set('year', $year);
        }
        $repository = $this
                ->getDoctrine()
                ->getManager()
                ->getRepository('HycAccountBundle:MonthEvent');

        $annuallist = $repository->monthListByYear($year);
        $jsonlist = json_encode($annuallist);
        return new Response($jsonlist);
    }

    //this part is to return entity to twig for using after
    $em = $this->getDoctrine()->getManager();
    $allimages = $em->getRepository('HycAccountBundle:TypeImage')
            ->findAll();
    return $this->render('HycAccountBundle:Account:index.html.twig', array('allimages' => $allimages));
}

here is my twig code:

 <script type="text/javascript">
    $(function() {
        $(document).ready(function (){
         jQuery.ajax({
                type: 'GET',
                cache: false,
                url: "{{ path('hyc_account_homepage') }}",
                success: function(data, textStatus, jqXHR) {
                    alert({{app.session.get('year')}});
                    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    //!!!!!!!!! here i can get year 2014 !!!!!!!!!!!!!!
                    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                },
                error:function (){
                alert('error!');
            }
            });
            return false;
        });
    });
</script>

but when i click pager part, for example, i click year 2013, then ajax call will return a number (data: 'nom=' + num) to controller, but there i reset session value to num normally, but i cant get alert correct session (it's always 2014 as the 1st time) here is the code ajax, almost same as above:

    <script type="text/javascript">
    $(document).ready(function(){
        //after paging, reload month
        $('#page-selection').bootpag({
            total: 3000,
            page: 2014,
            maxVisible: 5 
         }).on('page', function(event, num){
             jQuery.ajax({
                type: 'GET',
                url: "{{ path('hyc_account_homepage') }}",
                data: 'nom=' + num,
                success: function(data, textStatus, jqXHR) {
                    alert({{app.session.get('year')}});
                    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    //here is the problem, it's always 2014, not set again !!!!!!!!
                    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                },
                error:function (){
                alert('error!');
            }
            });
            return false;
         });
    });
</script>

hope u guys help me, thanks in advance, i've tried for almost 1 day and looked for almost all in google but find nothing = =

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX