PUT 请求被多次调用

PUT request being called more than once

我是 Ajax/jQuery 的新手,我正在做一项作业,我必须使用 http 方法 PUT 来更新 django 模型。我目前使用的代码除了 一个问题 之外有效:每次我点击新的修改按钮并提交时,PUT 请求被 运行 多次+1 次表格。

示例来自 console.log

(index):121 BUTTON: Pressed modify on product ID: 87
(index):133 GET: Looped through products for ID 87 and set the values in the modal accordingly
(index):153 PUT: Received put request for ID: 87
(index):121 BUTTON: Pressed modify on product ID: 88
(index):133 GET: Looped through products for ID 88 and set the values in the modal accordingly
(index):153 PUT: Received put request for ID: 87
(index):153 PUT: Received put request for ID: 88
(index):121 BUTTON: Pressed modify on product ID: 89
(index):133 GET: Looped through products for ID 89 and set the values in the modal accordingly
(index):153 PUT: Received put request for ID: 87
(index):153 PUT: Received put request for ID: 88
(index):153 PUT: Received put request for ID: 89

ajax 的代码:

//        MODIFYING A PRODUCT
product_ul.on("click", ".modify", function () { // On clicking modify
    var id = $(this).parents('li').attr('id'); // Get the PK which is the ID of the <li>
    console.log("BUTTON: Pressed modify on product ID: " + id);
    $.ajax({
        type: 'GET',
        url: 'get/',
        dataType: 'json',
        success: function (data) {
            $.each(data, function (key, value) { // Loop through all products from the json response
                if (value.id == id) { // If PK matches product PK from response
                    $('#dataModal').modal("show"); // Show modal
                    $('#edit_name').val(value.name); // Set the values
                    $('#edit_description').val(value.description);
                    $('#edit_price').val(value.price);
                    console.log("GET: Looped through products for ID " + id + " and set the values in the modal accordingly");
                }
            });
        }
    });
    $("#modify_product_form").submit(function (event) { // On submitting the modify form
        event.preventDefault(); // Prevent refresh

        var product_data = { // Get the values from the form
            name: $('#edit_name').val(),
            description: $('#edit_description').val(),
            price: $('#edit_price').val()
        };

        $.ajax({
            type: 'PUT',
            url: 'modify/' + id + '/',
            data: product_data,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                console.log("PUT: Received put request for ID: " + id);
            },
            success: function (product) {
                var product_html = "<li id=" + product.id + "><button class='delete btn btn-xs btn-danger'><span class='glyphicon glyphicon-remove'></span></button>" +
                    " <button class='modify btn btn-xs btn-warning'><span class='glyphicon glyphicon-cog'></span></button> " +
                    product.name + "</li>";
                $('#' + product.id).html(product_html); // Change the html to the modified version so it updates the list
                $('#dataModal').modal("hide"); // Hide the modal
            }
        });
    });
});

网页是这样的:

点击修改按钮后:

到目前为止,我唯一的假设是 $("#modify_product_form").submit(function (event)product_ul.on("click", ".modify", function () 内,因此导致了一些冲突,但我不知道如何在不嵌套的情况下获得 var id .

我希望 console.log 看起来像什么:

(index):121 BUTTON: Pressed modify on product ID: 87
(index):133 GET: Looped through products for ID 87 and set the values in the modal accordingly
(index):153 PUT: Received put request for ID: 87
(index):121 BUTTON: Pressed modify on product ID: 88
(index):133 GET: Looped through products for ID 88 and set the values in the modal accordingly
(index):153 PUT: Received put request for ID: 88
(index):121 BUTTON: Pressed modify on product ID: 89
(index):133 GET: Looped through products for ID 89 and set the values in the modal accordingly
(index):153 PUT: Received put request for ID: 89

如果我应该提供任何其他代码,请告诉我,如果您看到我的业余代码让您的眼睛受伤,我深表歉意!

140256257

您正在为您的表单 (// On submitting the modify form) 添加一个提交事件处理程序 您的产品点击处理程序 (// On clicking modify) 中。这意味着每次单击产品 ul 时,都会添加提交事件处理程序的新副本。提交表单后,所有这些副本都会被调用,并且它们都会发出 PUT 请求。

解决方案是将表单的提交处理程序移开,即移到单击处理程序之外。这样可以确保只添加一次。