A
A
Alexander Bondarenko2020-07-09 00:57:33
Django
Alexander Bondarenko, 2020-07-09 00:57:33

Why doesn't Ajax work in Django?

Trying to add to cart via ajax, getting 403 Forbidden error

<script>
    $( ".add_to_cart" ).submit(function(addr) {
        $.ajax({
        type: "POST",
        url: String($('.add_to_cart').data("action")),
        success: function() {
            $('.main-offer_list').html(`{% for item in cart %}
        {% with product=item.product %}
        <div class="main-offer_list-item">
            <div class="main-offer_list-info">
                <div class="main-offer_list-title">
                    {{ product.name }}
                </div>
                <!-- <div class="main-offer_list-calc">
                    1
                </div> -->
            </div>
            <img src="{{ product.image.url }}" alt="">
            <div class="main-offer_list-close">
                <a href="{% url 'cart:cart_remove' product.id %}"><img class="img-svg" src="{% static 'img/close.svg' %}" alt=""></a>
            </div>
        </div>
        {% endwith %}
        {% endfor %}`);
        } 
      });
});
</script>

How can I change my code, or what needs to be done so that the data is transferred?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-07-09
@bond_1013

Because you didn't bother to read the documentation .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question