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());
}