【问题标题】:XMLHttpRequest access denied while trying to access files from web server location - IE8尝试从 Web 服务器位置访问文件时 XMLHttpRequest 访问被拒绝 - IE8
【发布时间】:2020-01-24 05:41:46
【问题描述】:

我正在使用 xmlhttprequest 访问 url 路径的 javascript 工作。该代码适用于 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 internet-explorer internet-explorer-8 activex activexobject


    【解决方案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>
    

    输出:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      • 2011-01-17
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多