微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。