AJAX同时显示成功和错误消息

时间:2019-11-15 17:30:25

标签: jquery ajax

<script>
    function myFunction() {
        var useride = $("#userid").val();
        var category = $("#selectedcategory option:selected").text();
        var manufacturer = $("#selectedmanufacturer option:selected").text();
        var processor = $("#selectedprocessor option:selected").text();
        var model = $("#model").val();
        var ram = $("#ram").val();
        var serialNo = $("#serialNo").val();
        var serviceTagNo = $("#serviceTagNo").val();
        var supplier = $("#selectedsupplier option:selected").text();
        var date_indent = $("#date_indent").val();
        var date_approval = $("#date_approval").val();
        var date_issuance = $("#date_issuance").val();
        var date_purchase = $("#date_purchase").val();
        var vendor = $("#vendor").val();
        var approved_by = $("#approved_by").val();
        var warranty_expiry_date = $("#date_expiry").val();
        var price = $("#price").val();
        var recommended_by = $("#section option:selected").text();

        var markup = "<tr><td></td><td>" + category + "</td><td>" +
            manufacturer + "</td><td>" + processor + "</td><td>" + model +
            "</td><td>" + ram + "</td><td>" + serialNo + "</td><td>" +
            serviceTagNo + "</td><td>" + supplier + "</td><td>" +
            date_purchase + "</td><td>" + vendor + "</td><td>" +
            warranty_expiry_date + "</td><td>" + price + "</td><td>" + recommended_by + "</td>             </tr>";
        // ajax method starting here.
        //Current request reference; can be used else where
        var request;
        /* attach a submit handler to the form */
        $("#myForm").submit(function (event) {
            // abort any pending request
            if (request) {
                request.abort();
            }
            /* stop form from submitting normally */
            event.preventDefault();
            /*clear result div*/
            $("#result").html('');
            /* get some values from elements on the page: */
            var values = $(this).serialize();
            /* Send the data using post and put the results in a div */
            request = $.ajax({
                url: "/test/get_time.htm",
                type: "post",
                data: values,
                async:true,
                success: function () {
                    alert("submitted successfully");
                    $("table tbody").append(markup);
                    $('table tbody tr').each(function (idx) {
                        $(this).children(":eq(0)").html(idx);
                    });
                },
                error: function (xhr) {
                    alert("Same Serial No or Same Service Tag number already exists in the DB");
                    console.log(xhr);
                }
            });
        });
    }
</script>

问题在于,成功提交后,代码会显示成功消息,但是即使我由于尝试重复提交相同数据而收到错误消息,下一次每次成功提交数据时,也会出现错误消息还显示重复条目。我不知道原因。

0 个答案:

没有答案
相关问题