symfony2.4 ajax呼叫更新或设置会话问题 [英] symfony2.4 ajax call update or set session issues
问题描述
当我使用Ajax设置会话时,我对symfony2有问题,希望你们能为我提供帮助.
这是我的**控制器代码:**
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>
但是当我单击传呼机部分时,例如,我单击2013年,那么ajax调用将向控制器返回一个数字(数据:"nom =" + num),但是在正常情况下,我将会话值重置为num,但是我无法获取警报正确的会话(第一次始终是2014年),这里是代码ajax,与上面的代码几乎相同:
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) {
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//here is the problem, it's always 2014, not set again !!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
alert({{app.session.get('year')}});
},
error:function (){
alert('error!');
}
});
return false;
});
});
</script>
希望你们能帮助我,在此先感谢您,我已经尝试了将近1天,并在google中寻找了几乎所有内容,但是什么也没找到= =
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 = =
推荐答案
this- > getRequest()-> isXmlHttpRequest()){
this->getRequest()->isXmlHttpRequest()) {
分页 =
paging =
this-> getRequest()-> get( " );
this->getRequest()->get("nom");
这篇关于symfony2.4 ajax呼叫更新或设置会话问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!