How do I continue with the ajax request code?

  • 0
    The basketObject variable inside the refreshBasket () function is undefined. Async false / true tried it. Did not help. .done () looked but didn't figure out how to use in my case.
    let basket = {
            'get': function(){
                $.ajax({
                    type: 'post',
                    url: '/basket',
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    cache: false,
                    processData: false,
                    success: function (result) {
                        return result;
                    },
                });
            },
    // ... другие методы
    };
    
    // ... другой код
    
    function refreshBasket(){
            let basketObject = basket.get(); // вот тут basketObject undefined
            let totalProductsCount = 0;
            let totalBasketPrice = 0;
            $.each(basketObject, function (index, value){
                totalProductsCount += parseInt(value[1]);
                if(value.product.product_sale != null){
                    totalBasketPrice += parseInt(value[1]) * parseInt(value.product.product_sale);
                }else{
                    totalBasketPrice += parseInt(value[1]) * parseInt(value.product.product_price);
                }
            });
            console.log([totalProductsCount, totalBasketPrice]);
        }
    
        refreshBasket();
    AJAX Aiden Frey, Mar 19, 2020

  • 1 Answers
  • 0
    Because basket.get () returns nothing
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!