简体   繁体   中英

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. the code works fine with activexobject ( i dont want to use activex object ). when i try to call it using xmlhttprequest it does not work. it gives an error saying access denied. i am using IE8 version here. I have already tried the below workaround

  • enabling the "access data sources across domain in internet option"

  • 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. You need to host the web page on any web server. for testing purpose, I hosted this sample page on IIS server. Than you can try to access the web page from IE will help to visit the page without this error.

I tried to make a test with this sample code and I tested it with IE 11 (IE-8 document mode).

<!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:

在此处输入图像描述

Based on my testing result, code is working fine with the IE-8 document mode so it should also work in IE-8 browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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