Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

77
Views
Ajax call always going to error function in Struts 2

I am trying to return json from action by ajax call in struts 2 but it's always taking me to the error function. Here i am trying to take the cartsize by ajax call and want to print the cartsize whenever someone click on add to cart but it's taking me to error function always

Here is my ajax call

<script type="text/javascript">

            function addProductToCart(productId)
            {
                
                $.ajax({
                    url: 'addProductToCart',
                    method: "POST",
                    data: {productId: productId},
                    success: function (data) {
                        alert("In success");
                         $('#result').html(data.cartSize);
                         alert(data.cartSize);

                        
                    },
                    error: function (jqXHR, exception) {
                        console.log('Error occured!!');
                        alert("Error");
                    }
                });

                }
                </script>

My Action Class only the addProduct method

public String addProductToCart(){
        System.out.println("Cart: " + productId);
        MenuServices item = new MenuServices();
        if (sessionMap.get("Cart") == null) {
            HashMap<Integer, Menu> cart = new HashMap<>();

            try {
                Menu product = item.fetchProduct(productId);
                cart.put(productId, product);
                getSessionMap().put("Cart", cart);
            } catch (Exception ex) {
                Logger.getLogger(ReservationAction.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            HashMap cart = (HashMap) sessionMap.get("Cart");
            try {
                Menu product = item.fetchProduct(productId);
                cart.put(productId, product);
                getSessionMap().put("Cart", cart);
            } catch (Exception ex) {
                Logger.getLogger(ReservationAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        HashMap cart = (HashMap) sessionMap.get("Cart");
        setCartSize(cart.size());
        return ActionSupport.SUCCESS;
    }

Struts.xml

<package name="base" namespace="/" extends="json-default">
    <action name="addProductToCart" class="com.tablebooking.actions.ReservationAction" method = "addProductToCart">
    <result type="json">
        <param name="root">action</param>
    </result>
</action>
    </package>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!