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