简体   繁体   English

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

[英]JS - AJAX works on firefox but not on chrome

I have a problem with this code below. 我下面的这段代码有问题。 It runs perfectly on firefox v50.X but doesn't work on chrome v55.X. 它可以在Firefox v50.X上完美运行,但不能在chrome v55.X上运行。

HTML 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 name.html

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

Why is this code not working on chrome? 为什么此代码不适用于chrome?

Try this: 尝试这个:

function GetXmlHttpObject(){
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

var xmlhttp=GetXmlHttpObject();

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM