Symfony – GET /favicon.ico on ajax call

I’m working on a web application where users can add and delete stuff from their cart.

When the user wants to delete stuff from their cart, an ajax call is made. The problem is when they do it for the first time, I get the following error and the browser displays only the json

GET http://localhost:8000/favicon.ico

If they return to their cart and continue to delete stuff, there is no problem.

I can’t see why the first call lead to the server asking for /favicon.ico but not the following.

If needed here’s the ajax call

function onClickBtnRemove(event, idProduct, locale){
        console.log(idProduct);

        var total;
        var request = $.ajax({
            url: "/"+locale+"/shop/remove/"+idProduct,
            method: "GET",
            data: { },
            dataType: "json",
            sync:false,
            beforeSend : function(){
                console.log('helloo')
            },
            success: function(data){
                console.log(data);
                total = data.total;

                if($("#tr-"+idProduct).length){
                    $("#tr-"+idProduct).remove();
                }

                if(total == 0){
                    $('.table').remove();
                    var para = document.createElement("p");
                    $(para).attr('id','empty-cart-modal');
                    if(locale=='fr'){
                        $(para).text('Panier vide');
                    }else{
                        $(para).text('Cart empty');
                    }
                    $('.table-responsive').append(para);
                    $('.js-cart-not-empty').remove();

                    var empty = document.createElement("p");
                    $(empty).addClass('js-cart-empty');
                    if(locale=='fr'){
                        $(empty).text('Votre panier est vide');
                    }else{
                        $(empty).text('Your cart is empty');
                    }
                    $('.js-panier').append(empty);
                }else{
                    console.log('coucou');
                    console.log(total);
                    var quantity = 0;
                    for(var i=0; i<Object.keys(data.products).length; i++){
                        quantity += data.products[i].quantity;
                    }
                    $('.js-nb-articles').text(quantity);
                    $('.js-total').text(total);
                    $('#td-total').text(total);
                }
            }
        });
        /*axios.get("/"+locale+"/shop/remove/"+idProduct).then(function(response){
            console.log(response.data);
            total = response.data.total;

            if($("#tr-"+idProduct).length){
                $("#tr-"+idProduct).remove();
            }

            if(total == 0){
                $('.table').remove();
                var para = document.createElement("p");
                $(para).attr('id','empty-cart-modal');
                if(locale=='fr'){
                    $(para).text('Panier vide');
                }else{
                    $(para).text('Cart empty');
                }
                $('.table-responsive').append(para);
                $('.js-cart-not-empty').remove();

                var empty = document.createElement("p");
                $(empty).addClass('js-cart-empty');
                if(locale=='fr'){
                    $(empty).text('Votre panier est vide');
                }else{
                    $(empty).text('Your cart is empty');
                }
                $('.js-panier').append(empty);
            }else{
                console.log('coucou');
                console.log(total);
                var quantity = 0;
                for(var i=0; i<Object.keys(response.data.products).length; i++){
                    quantity += response.data.products[i].quantity;
                }
                $('.js-nb-articles').text(quantity);
                $('.js-total').text(total);
                $('#td-total').text(total);
            }
        });*/



    }

and the remove function

public function remove($id, CartService $cartService)
{

    $cartService->remove($id);
    $total = $cartService->getTotal();

    $products = $cartService->getFullCart();
    //dd($products);
    //dd(json_encode($product));
    $list = array();
    foreach ($products as $product) {
        $array = array(
            'id' =>  $product['product']->getId(),
            'name' => $product['product']->getName(),
            'description' => $product['product']->getDescription(),
            'price' => $product['product']->getPrice(),
            'quantity' =>$product['quantity'],
        );
        array_push($list,$array);
    }
    //dd($list);
    //dd($session->get('panier'));
    //return $this->redirectToRoute("shop");
    return $this->json([
            'total'=> $total,
            'products' => $list,
        ],200);
}

network request

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *