javascript - Get session value using JQueryAjax - Stack Overflow

I'm a newbie in this, I'm trying to refresh a menu value (cart content) without reloading the

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
Add a ment  | 

2 Answers 2

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745504190a4630553.html

相关推荐

  • javascript - Get session value using JQueryAjax - Stack Overflow

    I'm a newbie in this, I'm trying to refresh a menu value (cart content) without reloading the

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信