我目前在使 ajax 与低于 10 的 IE 版本一起工作时遇到了很多麻烦,而我对 Firefox、Chrome、Safari、Opera 甚至 IE 10 都没有问题。我的网站只显示了 3 个链接在一起的选择菜单,其中 2 个被填满每当在第一个然后在第二个中选择某些内容时,数据都来自 mysql 数据库。使用 IE 8 或 9 时,Apache 日志不显示任何 POST 请求正在完成,并且我的选择菜单不会填充数据。
这是我的 JavaScript 代码:
function getXMLHttpRequest() {
var xhr = null;
if (window.XMLHttpRequest || window.ActiveXObject) {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
} else {
xhr = new XMLHttpRequest();
}
} else {
alert("Your browser doesn't support XMLHTTPRequest...");
return null;
}
return xhr;
}
function request(oSelect) {
var value = oSelect.options[oSelect.selectedIndex].value;
var xhr = getXMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
displayOptions(xhr.responseText, oSelect);
}
}
xhr.open("POST", "ajax.pl", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(oSelect.name + "=" + value);
}
function displayOptions(oData, oSelect) {
if (oSelect.name == "genus") {
document.getElementsByName("species")[0].disabled = false;
document.getElementsByName("species")[0].innerHTML = oData;
document.getElementsByName("subspecies")[0].disabled = true;
document.getElementsByName("subspecies")[0].innerHTML = "";
}
if (oSelect.name == "species") {
document.getElementsByName("subspecies")[0].disabled = false;
document.getElementsByName("subspecies")[0].innerHTML = oData;
}
}
到目前为止,我已经尝试将 xhr.open 命令从 POST 更改为 GET,但没有任何运气。我还尝试将 perl cgi 的绝对路径放在 xhr.send 命令中,但对 IE 8 完全没有任何影响。
我也尝试过以下事情:
xhr.onreadystatechange = function() {
if(xhr.readyState == 0) {
alert("zero");
}
if(xhr.readyState == 1) {
alert("one");
}
if(xhr.readyState == 2) {
alert("two");
}
if(xhr.readyState == 3) {
alert("three");
}
if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
alert("four");
displayOptions(xhr.responseText, oSelect);
}
}
这为我提供了 Chrome 的以下输出:
one
two
three
four
虽然我有这个 IE 8:
one
one
four
由于我是新手,我找不到可能导致此问题的原因。我一直在网上寻找答案,但仍然没有找到答案。
当我开始绝望时,任何人都会非常感激。