我开发了一个在 chrome 和 firefox 上运行良好的网络应用程序。然而,当谈到测试时间时,它在 IE 中无法正常工作。它只是似乎并没有真正得到请求?
这是 Javascript 代码:
function createXMLHttpRequest() {
var xmlhttp = false;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if(window.ActiveXObject) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlhttp = false;
}
}
}
return xmlhttp;
};
function checkForData(){
var target = document.getElementById('accordion');
var loading = document.getElementById('controls');
var xhr = createXMLHttpRequest();
loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Querying the Database';
xhr.open('GET','getData.php',true);
xhr.send(null);
xhr.onload = function(){
loading.innerHTML = '<a href="../classes/php/print.php" />Print Report</a><br/><br/><a href="../index.php" />HomePage</a><br/><a href="../classes/php/actionedClass.php" />Refresh</a><br/><a href="../classes/php/logout.php" />Logout</a><br/>';
target.innerHTML = xhr.responseText;
// addPrettyness();
};
xhr.onerror = function (){
target.innerHTML = 'Failed :(';
};
}
function addPrettyness(){
$(function() {
var stop = false;
$( "#accordion h3" ).click(function( event ) {
if ( stop ) {
event.stopImmediatePropagation();
event.preventDefault();
stop = false;
}
});
$( "#accordion" )
.accordion({
collapsible: true,
header: "> div > h3"});
});
}
function checkAgain(){
setInterval('checkForData()',30000); //Every 30 seconds run the check for new data script
}
function changeAction(action, actionChangeId){
xhr = new XMLHttpRequest();
if(action == 0){
var loading = document.getElementById('controls');
loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Sending to the Database';
xhr.open('GET','../classes/php/actionNo.php?actionChangeId='+actionChangeId,true);
xhr.send();
xhr.onload = function(){
checkForData();
};
xhr.onerror = function(){
alert('Failed to change action... Try again please!');
};
}else if(action == 1){
xhr = new XMLHttpRequest();
var loading = document.getElementById('controls');
loading.innerHTML = '<img src="../images/loading.gif" width=20 height=20 />Sending to the Database';
xhr.open('GET','../classes/php/actionYes.php?actionChangeId='+actionChangeId,true);
xhr.send();
xhr.onload = function(){
checkForData();
};
xhr.onerror = function(){
alert('Failed to change action... Try again please!');
};
}
}
function startEngine(){
checkForData();
//checkAgain();
}
window.onload = startEngine;
它从 echos 请求的 .php 文件将字符串结果返回给 javascript。
最佳答案
如前所述here , Internet Explorer 仅从版本 9 开始支持 XMLHttpRequest 对象的 onload
事件。
因此,对于 IE 8 及以下版本,您可以使用老式的方式进行操作:
xhr.onreadystatechange = function()
{
//ready?
if (xhr.readyState != 4)
return false;
//get status:
var status = xhr.status;
//maybe not successful?
if (status != 200) {
alert("AJAX: server status " + status);
return false;
}
//Got result. All is good.
loading.innerHTML = '<a href="../classes/php/print.php" />Print Report</a>' +
'<br/><br/><a href="../index.php" />HomePage</a><br/>' +
'<a href="../classes/php/actionedClass.php" />Refresh</a><br/>' +
'<a href="../classes/php/logout.php" />Logout</a><br/>';
target.innerHTML = xhr.responseText;
return true;
}
关于php - XMLHttpRequest 在 IE 7/8 中不起作用,但在其他浏览器中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5418403/