XMLHttpRequest 只允许每页重新加载一个请求

问题描述

我有一个页面,它有 3 个不同的按钮,它们执行 3 个 XMLHttpRequests 来查询数据库并填充一个表。当我加载页面时,我可以点击 3 个按钮中的任何一个,它们各自的 jquery 函数会触发请求,但是,为了再次使用任何按钮,它让我首先重新加载页面,这显然是违反直觉的.为什么它需要这个,我如何使它不需要重新加载?

$( document ).ready(function() {
if($('#pending-table tr').length == 1) {
    $("#pending-table").hide();
    $("#pending-table-title").hide();
}


$('.allow').click(function() {
    var id = this.id;

    var xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var row = document.getElementById(id + "t");
            row.parentNode.removeChild(row);

            if($('#pending-table tr').length == 1) {
                $("#pending-table").hide();
                $("#pending-table-title").hide();
            }
            $("#user-table").replaceWith(this.responseText);
        }
    };
    xhttp.open("GET","allow.PHP?email=" + id,false);
    xhttp.send();
});

$(".remove").click(function() {
    var id = this.id;

    var xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            $("#user-table").replaceWith(this.responseText);
        }
    };
    xhttp.open("GET","remove.PHP?email=" + id,false);
    xhttp.send();
});

$(".change-role").click(function() {
    var id = this.id;

    var xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            $("#user-table").replaceWith(this.responseText);
        }
    };
    xhttp.open("GET","change-perm.PHP?email=" + id,false);
    xhttp.send();
});

});

在 jquery 点击函数中没有 XMLHttpRequest 时,它运行的次数不受限制。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)