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:**
public function indexAction() {
if ($this->getRequest()->isXmlHttpRequest()) {
$paging = $this->getRequest()->get("nom");
$session = $this->getRequest()->getSession();
if ($paging) {
$year = $paging;
$session->set('year', $year);
} else {
$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);
}
$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')}});
},
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(){
$('#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')}});
},
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 = =