为什么我的 XHTML 文件在 IE 中被拒绝访问

Why does my XHTML file get an access denied in IE

提问人:Natacha BK 提问时间:8/7/2020 更新时间:8/7/2020 访问量:133

问:

给出以下代码:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Informations</title>

<script language="JavaScript">

function chargeDocument(URI) {
try {
 
 xmlhttp = new XMLHttpRequest();
 xmlhttp.open("GET", URI,true);
 xmlhttp.onreadystatechange=function() {
 if (xmlhttp.readyState==4) {
  afficheTitres(xmlhttp.responseXML);
  }
 }
 xmlhttp.send(null);
 } catch(o) {alert(o);}
}

function afficheTitres(doc) {
      titres = doc.getElementsByTagName("title");
      elementol = document.createElement("ol");
      var longueur = titres.length;
      for ( k = 0; k &lt; longueur ; ++k) {
        elementli = document.createElement("li");
        elementli.appendChild(
          document.createTextNode(
            titres[k].firstChild.nodeValue
          )
        );
        elementol.appendChild(elementli);
      }
      body = document.getElementsByTagName("body").item(0);
      body.appendChild(elementol);
}
</script>
</head>
<body>
<ul>
<li><a href="javascript:chargeDocument('infos.xml');">By clicking there, you'll have the info</a></li>
</ul>
</body>
</html>

当我用IE打开xhtml文件时,它给了我“通过单击那里,您将获得信息”,但是当我单击链接时,会弹出“拒绝访问”。这是为什么呢?

XML Internet Explorer XHTML 访问被拒绝

评论

0赞 Alohci 8/7/2020
本地 XHTML 文件,还是来自 Web 服务器?
0赞 Natacha BK 8/7/2020
本地 XHTML 文件
0赞 Alohci 8/7/2020
这是跨域资源共享 (CORS) 的一个不方便的功能。浏览器将其他本地资源(如 infos.xml 文件)视为来自不同来源,并且由于没有授予(或可授予,据我所知)访问来自不同来源的资源的权限,因此访问被拒绝。最好的解决方案是使用简单的 Web 服务器来提供文件。

答:

1赞 Yu Zhou 8/7/2020 #1

我同意 Alohci 的观点。它不能像在任何浏览器中那样通过本地文件协议访问。如果我将文件托管在远程服务器或本地主机中,则它可以通过单击文本来显示信息。D:\infos.xmlhttp://...

因此,您需要将其托管在 Web 服务器中才能访问资源。您可以查看结果:

enter image description here

评论

0赞 Natacha BK 8/7/2020
谢谢!这奏效了。我为 chrome 安装了一个 Web 服务器并用它打开了文件。