【问题标题】:Javascript/AJAX function that works in Internet Explorer, but not in FirefoxJavascript/AJAX 函数适用于 Internet Explorer,但不适用于 Firefox
【发布时间】:2012-12-12 12:30:33
【问题描述】:

我有这两个按钮,用户可以单击它们来批准/拒绝站点上的某人,并且代码在 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();
        }

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

【问题讨论】:

  • 我可以指出您实际上并没有验证 GET 请求是否成功完成。仅当更改成功时,您才应该重新加载。

标签: javascript ajax


【解决方案1】:

你有一个竞争条件!

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

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

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

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

【讨论】:

  • 哦!我完全错过了!谢谢!...只是出于好奇,4的就绪状态是什么?
猜你喜欢
  • 1970-01-01
  • 2011-01-04
  • 2020-06-05
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多