problem with ajax

hi

i have this code:




    $(document).ready(function(){

        $('a#add-to-card').on('click', function(e){

            e.preventDefault();

            $itemId = $(this).data('item-id');

            $.ajax({

                    type: 'POST',

                    dataType: 'json',

                    url: '<?php Yii::$app->urlManager->createUrl(["master-category/addToCardAjax"]); ?>',

                    data: {'item-id': $itemId},

                    success: function(data){

                        console.log(data['count']);

                    },

                });

        });

    });



my action:




    public function addToCardAjaxAction(){

        $cardSession = new \yii\web\Session;

        $cardSession->open();

        if(!isset($cardSession['cardCount'][$_POST['item_id']]))

            $cardSession['cardCount'][$_POST['item_id']] = 0;

        else

            $cardSession['cardCount'][$_POST['item_id']]++;


        $this->renderPartial('addtocardajax');

        //echo json_encode($cardSession);

    }



and view file:




<?php

session_start();

echo json_encode(array('count' => count($_SESSION['cardCount'])));



when i use json type, no error and no result but when not using json type it will return all my site

another question is why i have to start session? when dont use session_start i will get an error say _SESSION not known

what should i do?

thanks

answer