简体   繁体   English

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

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

I am working on javascript where i am trying to access a url path using xmlhttprequest.我正在研究 javascript,我正在尝试使用 xmlhttprequest 访问 url 路径。 the code works fine with activexobject ( i dont want to use activex object ).该代码适用于activexobject(我不想使用activex object)。 when i try to call it using xmlhttprequest it does not work.当我尝试使用 xmlhttprequest 调用它时,它不起作用。 it gives an error saying access denied.它给出了一个错误,说访问被拒绝。 i am using IE8 version here.我在这里使用 IE8 版本。 I have already tried the below workaround我已经尝试过以下解决方法

  • enabling the "access data sources across domain in internet option"启用“在 Internet 选项中跨域访问数据源”

  • adding trusted sites添加受信任的站点

 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 }

Here, you got Access denied error.在这里,您收到拒绝访问错误。 Looks like you are directly trying to run the HTML page in IE browser.看起来您直接尝试在 IE 浏览器中运行 HTML 页面。 You need to host the web page on any web server.您需要在任何 web 服务器上托管 web 页面。 for testing purpose, I hosted this sample page on IIS server.出于测试目的,我在 IIS 服务器上托管了这个示例页面。 Than you can try to access the web page from IE will help to visit the page without this error.您可以尝试从 IE 访问 web 页面将有助于访问该页面而不会出现此错误。

I tried to make a test with this sample code and I tested it with IE 11 (IE-8 document mode).我尝试使用此示例代码进行测试,并使用 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>

Output: Output:

在此处输入图像描述

Based on my testing result, code is working fine with the IE-8 document mode so it should also work in IE-8 browser.根据我的测试结果,代码在 IE-8 文档模式下运行良好,因此它也应该在 IE-8 浏览器中运行。

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

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