0

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()});
        }
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. Commented Jan 10, 2014 at 23:19
  • Exactly how can I retrieve session value from the response ? Commented Jan 10, 2014 at 23:26
  • You can't. Your server would send the session value AS the response. Commented Jan 10, 2014 at 23:26

2 Answers 2

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.com/spring/spring-mvc/ajax-spring-mvc-3-annonations-jquery/

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.