Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

81
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda