admin管理员组文章数量:1433161
I'm a newbie in this, I'm trying to refresh a menu value (cart content) without reloading the whole page.
This is my issue : ${cartSession.getCartContent()} value in the alert check is Undefinied.
If it could help, In server side I'm using Spring.
$(document).ready(function(){
var $form = $("#panierform");
$form.submit(function(){
$.post($(this).attr('action'), $(this).serialize(), function(response){
},'json');
alert("Ajouté avec succès !");
refreshCartValue();
return false;
});
});
function refreshCartValue() {
alert(${cartSession.getCartContent()});
$("#cartValue").text("");
$("#cartValue").text(${cartSession.getCartContent()});
}
I'm a newbie in this, I'm trying to refresh a menu value (cart content) without reloading the whole page.
This is my issue : ${cartSession.getCartContent()} value in the alert check is Undefinied.
If it could help, In server side I'm using Spring.
$(document).ready(function(){
var $form = $("#panierform");
$form.submit(function(){
$.post($(this).attr('action'), $(this).serialize(), function(response){
},'json');
alert("Ajouté avec succès !");
refreshCartValue();
return false;
});
});
function refreshCartValue() {
alert(${cartSession.getCartContent()});
$("#cartValue").text("");
$("#cartValue").text(${cartSession.getCartContent()});
}
Share
Improve this question
edited Jan 10, 2014 at 23:25
GSDa
asked Jan 10, 2014 at 23:16
GSDaGSDa
1932 gold badges6 silver badges21 bronze badges
3
-
2
unfortunately it isn't possible the way you're doing it because
${cartSession.getCartContent()}
can only execute on the server, therefore it will only ever contain the value that was originally returned with the current page. You'll need to instead return that value from the server when you perform the ajax request, then access it within the success of that ajax request. – Kevin B Commented Jan 10, 2014 at 23:19 - Exactly how can I retrieve session value from the response ? – GSDa Commented Jan 10, 2014 at 23:26
- You can't. Your server would send the session value AS the response. – Kevin B Commented Jan 10, 2014 at 23:26
2 Answers
Reset to default 2 alert(${cartSession.getCartContent()});
You can't call a server side Java method from Javascript.
Javascript executes on the client browser, Java on the server.
What you can do is handle form posting VIA Ajax. Make a POST request to server, return the actual response(with success/failure flags) and do whatever you want with it via JQuery/Javscript:
Follow this simple example with Spring: http://www.raistudies./spring/spring-mvc/ajax-spring-mvc-3-annonations-jquery/
Thank you for your response, very useful tutorial, here is my code it does work now :
$(document).ready(function(){
var $form = $("#panierform");
$form.submit(function(e){
$.post($(this).attr('action'), $(this).serialize(), function(response){
alert("Ajouté avec succès !");
refreshCartValue(response);
},'json');
e.preventDefault();
return false;
});
});
function refreshCartValue(response) {
$("#cartValue").text("");
$("#cartValue").text(response);
}
Be sure to add e.preventDefault();
what I missed before was the return value as ResponseBody from the controller :
@RequestMapping("pages/addToCart")
public @ResponseBody String addToCart(HttpSession session, @RequestParam String produitId, @RequestParam Long quantite){
//
return String.valueOf(cart.getCartContent());
}
本文标签: javascriptGet session value using JQueryAjaxStack Overflow
版权声明:本文标题:javascript - Get session value using JQueryAjax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745504189a2661169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论