javascript - PUT 请求被多次调用

标签 javascript jquery ajax django put

我是 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 button

点击修改按钮后:

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

最佳答案

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

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

关于javascript - PUT 请求被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47187142/

相关文章:

javascript - 在 Visual Studio Code 中突出显示未定义的 Javascript 变量

javascript - 嵌套路由和 Switch - 如何传递 props.match.params.id?

javascript - 带 D3 图的 Bootstrap Modal-Dialog

javascript - 通过javascript将图像路径添加到css中的过滤器

javascript - 通过 $.extend 从父类继承

javascript - 语法错误: Invalid character in jQuery $. ajax调用在哪里?

javascript - 如何为正在查看的网站动态更改 css 样式表?

javascript - 从 slickgrid 内的按钮 Bootstrap 模式

javascript - DateTimePicker Redux 表单字段问题

javascript - 同步请求和异步请求有什么区别? (异步=真/假)