JS - AJAX适用于Firefox,但不适用于chrome

时间:2017-01-12 07:30:32

标签: javascript html ajax html5

我在下面的代码中遇到了问题。它在firefox v50.X上运行完美,但在chrome v55.X上无效。

HTML

<!doctype html>
<html>
<head>
    <script>
        function load(){
        if(window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("container").innerHTML = xmlhttp.responseText;
        }
        }
        xmlhttp.open("GET", "name.html", true);
        xmlhttp.send();
        }
    </script>
</head>
<body>
   <input type="submit" onclick="load();">
   <div id="container"></div>
</body>
</html>

name.html

<html>
<body>
    <p>Test.</p>
</body>
</html>

为什么这段代码不能用于Chrome?

1 个答案:

答案 0 :(得分:0)

试试这个:

{{1}}

Source