Skip to content

JS 防止 AJAX 二次提交

🏷️ JavaScript

js
var currentRequests = {};
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    var key = options.url;
    if (!currentRequests[key]) {
        currentRequests[key] = jqXHR;
    } else {
        jqXHR.abort();//放弃后触发的提交  
        //currentRequests[key].abort();//放弃之前触发的提交  
    }
});

function ajaxTest1() {
    $.ajax({
        url: "/Home/Index1",
        type: "POST",
        data: { id: "提交请求" },
        dataType: 'json',
        success: function (data) {
            if (data.state) {                     
            }
            flag = 1;
        },
        complete: function (data) {
            delete currentRequests[this.url];
        }
    });
}