2

我正在尝试通过 ajax 将 php 文件加载到 div 中。它在除 IE6 之外的所有浏览器中都能正常工作(它不加载 php 文件)。我的任务是它也需要在 IE6 中工作。请提出更正建议。

我的index.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript">
window.onload = function(){
document.getElementById("aside").innerHTML="<img src='loadingImage.gif'>";
if(XMLHttpRequest) var x = new XMLHttpRequest();
else var x = new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET", "other_content_1.php", true);
x.send("");
x.onreadystatechange = function(){
    if(x.readyState == 4){
        if(x.status==200) document.getElementById("aside").innerHTML = x.responseText;
        else document.getElementById("aside").innerHTML = "Error loading document";
        }
    }
} 
</script>
</head>

<body>
<div id="aside">This is other content</div>
</body>
</html>

我的other_content_1.php文件:

<div id='other-content-1'>
<?php echo 'This text is loading via php command'; ?>
</div>
4

2 回答 2

1

IE6 在这一行抛出 javascript 错误:

if(XMLHttpRequest)

这是适用于 IE6(也可能适用于 IE5.5)的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript">
window.onload = function(){

    document.getElementById("aside").innerHTML="<img src='loadingImage.gif'>";

    var x = null;

    if (window.XMLHttpRequest) {
        var x = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        var x = new ActiveXObject('MSXML2.XMLHTTP.3.0');
    } else {
        // fallback
    }

    x.open("GET", "other_content_1.php", true);
    x.send("");
    x.onreadystatechange = function() {

        if(x.readyState == 4) {
            if(x.status==200) 
                document.getElementById("aside").innerHTML = x.responseText;
            else 
                document.getElementById("aside").innerHTML = "Error loading document";
        }
    }
} 
</script>
</head>

<body>
<div id="aside">This is other content</div>
</body>
</html>
于 2013-06-30T17:30:07.627 回答
1

根据Microsoft docsonreadystatechange , IE 7 中引入了对的支持;它在 IE 6 中不起作用。解决方法是执行同步请求并直接使用结果:

if(window.XMLHttpRequest) {
    var x = new XMLHttpRequest();
    x.open("GET", "other_content_1.php", true);
    x.send("");
    x.onreadystatechange = function(){
        if(x.readyState == 4){
            if(x.status==200) document.getElementById("aside").innerHTML = x.responseText;
            else document.getElementById("aside").innerHTML = "Error loading document";
            }
        }
    }
} else {
    // assume IE 6
    var x = new ActiveXObject("Microsoft.XMLHTTP");
    x.open("GET", "other_content_1.php", false); // <- note change to last arg
    x.send("");
    if(x.readyState == 4){
        if(x.status==200) document.getElementById("aside").innerHTML = x.responseText;
        else document.getElementById("aside").innerHTML = "Error loading document";
        }
    }
}
于 2013-06-30T17:20:13.570 回答