简体   繁体   English

如何减少Jquery / Ajax代码->删除重复项

[英]How to reduce the Jquery/Ajax code -> remove duplication

I have this Jquery/Ajax code which sends GET requests and I think there is a lot of duplication. 我有这个发送GET请求的Jquery / Ajax代码,我认为有很多重复项。 How to reduce this code? 如何减少此代码?

$(".add_to_cart").click(function() {
    product_slug = $(this).attr("data-slug")
    data = {
        product_slug: product_slug,
    },

    $.ajax({
        type: "GET",
        url: "{% url 'cart:cart_create' %}",
        data: data,
        success: function(data) {
            $(".cart_score").html(data.cart_length + " товаров " + data.cart_total + " ₽")
        },
    });
});

$(".update_cart").click(function() {
    product_slug = $(this).attr("data-slug")
    quantity = $(this).val()
    data = {
        product_slug: product_slug,
        quantity: quantity,
    }
    $.ajax({
        type: "GET",
        url: "{% url 'cart:cart_update' %}",
        data: data,
        success: function(data) {
            $(".cart_score").html(data.cart_length + " товаров " + data.cart_total + " ₽")
            $(".cart_total").html(data.cart_total + " ₽")
            $(".item_total_price-"+product_slug).html(data.cart_price + " ₽")
        },
    });
});

$(".del_from_cart").click(function() {
    product_slug = $(this).attr("data-slug")
    data = {
        product_slug: product_slug,
    },

    $.ajax({
        type: "GET",
        url: "{% url 'cart:cart_delete' %}",
        data: data,
        success: function(data) {
            $(".cart_score").html(data.cart_length + " товаров " + data.cart_total + " ₽")
            $(".cart_total").html(data.cart_total + " ₽")
            $(".item_product-"+product_slug).css("display", "none")

            if (data.cart_length < 1) {
                $(".cart_score").html("<a href='{% url 'cart:cart_show' %}' class='cart_score'>Корзина пустая</a>")
                $(".cart_block").html("<p>Корзина пуста <a href='{% url 'shop:product_list' %}'>выбрать модель</a></p>")
            }
        },
    });
});

$(".clear_cart").click(function() {
    $.ajax({
        type: "GET",
        url: "{% url 'cart:cart_clear' %}",
        success: function() {
            $(".cart_score").html("<a href='{% url 'cart:cart_show' %}' class='cart_score'>Корзина пустая</a>")
            $(".cart_block").html("<p>Корзина пуста <a href='{% url 'shop:product_list' %}'>выбрать модель</a></p>")
        },
    });
});

I'm new to Javascript, any ideas? 我是Java新手,有什么想法吗? at least a small example :) 至少是一个小例子:)

Create One Common Generic Ajax Call this way 用这种方式创建一个通用的Ajax

You can Refer more From here 您可以从这里参考更多

function CommonAjaxCall(type, url, parameters, successCallback) {

                $.ajax({
                    type: type,
                    url: url,
                    data: JSON.stringify(parameters),
                    contentType: 'application/json;',
                    dataType: 'json',
                    success: successCallback,
                    error: function(xhr, textStatus, errorThrown) {
                        console.log('error');
                    }
                });
            }

CommonAjaxCall(type, url, pars, onSuccess);

function onSuccess(result) {
//perform your logic from here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM