我目前有很多麻烦,使AJAX与IE版本低于10,而我没有问题与Firefox,铬,Safari浏览器,歌剧院和甚至IE 10.我的网站只显示3个链接在一起的选择菜单,其中2个从MySQL数据库获取数据,只要在第一个数据库中选择了某些内容,然后在第二个数据库中选择。使用IE 8或9时,Apache日志不会显示任何POST请求,我的选择菜单不会被数据填充。JavaScript的XMLHttpRequest将不会与IE 8或9发送,但与IE 10的作品
这是我的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;
}
}
到目前为止,我已经试图从POST更改xhr.open命令来获取,但没有任何运气。我也尝试在xhr.send命令中放入perl cgi的绝对路径,但完全不会影响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
由于我是新手,我无法找到可能导致此问题的原因。我一直在网络上寻找答案,但还没有找到答案。
随着我开始绝望,任何人都会非常感激。
这就是为什么JS框架存在.. http://api.jquery.com/ jQuery.ajax/ – Oscar
我认为旧的IE不喜欢'.innerHTML'被用来填充'select'元素。 –
为什么首先测试'window.ActiveXObject()' - IE从版本7开始支持'window.XMLHttpRequest'(反正基本支持)。如果你在另一个函数中不测试'null',为什么还要在浏览器不支持的时候编写一个'null'返回值呢? @CrazyTrain - 虽然不会阻止Ajax请求到达Web服务器。 – nnnnnn