尝试从 Web 服务器位置访问文件时拒绝 XMLHttpRequest 访问 - IE8

XMLHttpRequest access denied while trying to access files from web server location - IE8

提问人:Divakar R 提问时间:9/24/2019 更新时间:9/24/2019 访问量:383

问:

我正在研究javascript,我尝试使用xmlhttprequest访问url路径。该代码适用于ActiveXObject(我不想使用ActiveX对象)。当我尝试使用 xmlhttprequest 调用它时,它不起作用。它给出一个错误,说访问被拒绝。我在这里使用 IE8 版本。我已经尝试了以下解决方法

  • 启用“在 Internet 中跨域访问数据源选项”

  • 添加受信任的站点

if(src) //scr = templates/mytemplate
{
	try{
	var xhr= new XMLHttpRequest();   //new ActiveXObject("Microsoft.XMLHTTP"); works fine 
xhr.onreadystatechange=function()
{
	if(xhr.readyState==4)
{
  log.profile(src);
if(xhr.status==200||xhr.status==0)
{
	//do some action
}
}

element.html(xhr.responseText);
log.profile(src);
xhr.open("GET",src,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(null);
}}catch(e){
	alert("unable to load templates"+e); // here i am getting error saying acess denaied 
}

javascript 互联网浏览器 8 activex activex 对象

评论


答:

0赞 Deepak-MSFT 9/24/2019 #1

在这里,您收到了“拒绝访问”错误。看起来您正在直接尝试在 IE 浏览器中运行 HTML 页面。您需要在任何 Web 服务器上托管网页。出于测试目的,我在IIS服务器上托管了此示例页面。比你可以尝试从IE访问网页将有助于访问该页面而不会出现此错误。

我尝试使用此示例代码进行测试,并使用 IE 11(IE-8 文档模式)对其进行了测试。

<!DOCTYPE html>
<html>
<body>

<h2>Using the XMLHttpRequest Object</h2>

<div id="demo">
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</div>

<script>
function loadXMLDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "xmlhttp_info.txt", true);
  xhttp.send();
}
</script>

</body>
</html>

输出:

enter image description here

根据我的测试结果,代码在 IE-8 文档模式下运行良好,因此它也应该在 IE-8 浏览器中运行。