在 Internet Explorer 中有效但在 Firefox 中无效的 Javascript/AJAX 函数

标签 javascript ajax

我有这两个按钮,用户可以单击它们来批准/拒绝网站上的某人,代码在 IE 中运行完美,但是当我尝试使用 firefox 时,单击按钮时什么也没有发生。

javascipt/ajax 代码是:

        function ApproveOrDenyStudent(i, action){
            if (window.XMLHttpRequest){
                // code for IE7+, Firefox, Chrome, Opera, Safari                
                xmlhttp=new XMLHttpRequest();
            }
            else{
                // code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }                                                

            var newStudentEmail = "newStudentEmail" + i;

            var emailField = document.getElementById(newStudentEmail);
            var email = emailField ? emailField.value : '';       



            // Approve/deny the user
            if (action == 0){      
                xmlhttp.open("GET","ApproveStudent.php?email="+email,true); 
            }
            else if (action == 1){
                xmlhttp.open("GET","DenyStudent.php?email="+email,true); 
            }
            xmlhttp.send();
            window.location.reload();
        }

任何帮助都会很棒!谢谢!

最佳答案

你遇到了竞争条件!

xmlhttp.send();
window.location.reload();

您正在进行异步调用。您正在发出 Ajax 请求并立即替换页面!对服务器的调用没有发出。

请求完成后重新加载页面。

xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4){
        window.location.reload(true);
    }
};
xmlhttp.send();

关于在 Internet Explorer 中有效但在 Firefox 中无效的 Javascript/AJAX 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13832774/

相关文章:

javascript - 如何验证从此 javascript 动态生成的字段?

javascript - array.pop()的返回值;

ajax - 如何在ajax调用中使用wordpress函数

javascript - 为什么我的脚本无法加载 jQuery Mobile?

javascript - 如何为 html 创建 iPhone 联系人标题滚动效果?

javascript - 求背景全景插件

java - 使用 jsp 将 javascript 变量添加到 session 属性

javascript - 如何在 iframe 上下文中使用 jQuery 方法

javascript - 无法读取 null 的属性 addEventListener

jquery - 如何克隆 StackOverFlow JQuery 接口(interface)?