Javascript 错误:“访问受限 URI 被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6653825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:38:39  来源:igfitidea点击:

Error: "Access to restricted URI denied"

javascripthtmlxmlfirefoxfirebug

提问by Bala

Access to restricted URI denied" code: "1012 [Break On This Error]

xhttp.send(null);

访问受限 URI 被拒绝”代码:“1012 [Break On This Error]

xhttp.send(null);

function getXML(xml_file) {

  if (window.XMLHttpRequest) {

    var xhttp = new XMLHttpRequest();  // Cretes a instantce of XMLHttpRequest object
  }
  else {

    var xhttp = new ActiveXObject("Microsoft.XMLHTTP");  // for IE 5/6
  }

  xhttp.open("GET",xml_file,false);  
  xhttp.send(null);  

   var xmlDoc = xhttp.responseXML; 

   return (xmlDoc);
}

I'm trying to get data from a XML file using JavaScript. Im using Firebug to test and debug on Firefox.

我正在尝试使用 JavaScript 从 XML 文件中获取数据。我使用 Firebug 在 Firefox 上进行测试和调试。

The above error is what I'm getting. It works in other places i used the same before, why is acting weird here?

上面的错误是我得到的。它在我以前用过的其他地方也有效,为什么在这里表现得很奇怪?

Can someone help me why it's occuring?

有人可以帮助我为什么会发生这种情况吗?

Update:

更新:

http://jquery-howto.blogspot.com/2008/12/access-to-restricted-uri-denied-code.html

http://jquery-howto.blogspot.com/2008/12/access-to-restricted-uri-denied-code.html

I found this link explaining the cause of the problem. But I didn't get what the solution given means can someone elaborate?

我发现这个链接解释了问题的原因。但是我没有得到给出的解决方案意味着有人可以详细说明吗?

回答by Kevin B

Another possible cause of this is when you are working with a .html file directly on the file system. For example, if you're accessing it using this url in your browser: C:/Users/Someguy/Desktop/MyProject/index.html

另一个可能的原因是当您直接在文件系统上使用 .html 文件时。例如,如果您在浏览器中使用此 url 访问它:C:/Users/Someguy/Desktop/MyProject/index.html

If that then has to make an ajax request, the ajax request will fail because ajax requests to the filesystem are restricted. To fix this, setup a webserver that points localhost to C:/Users/Someguy/Desktop/MyProjectand access it from http://localhost/index.html

如果然后必须发出 ajax 请求,则 ajax 请求将失败,因为对文件系统的 ajax 请求受到限制。要解决此问题,请设置一个指向 localhostC:/Users/Someguy/Desktop/MyProject并从中访问它的网络服务器http://localhost/index.html

回答by epascarello

Sounds like you are breaking the same origin policy.

听起来您违反了同源政策

Sub domains, different ports, different protocols are considered different domains.

子域、不同的端口、不同的协议被认为是不同的域。

回答by zatatatata

Try adding Access-Control-Allow-Origin:*header to the server side script that feeds you the XML. If you don't do it in PHP (where you can use header()) and try to read a raw XML file, you probably have to set the header in a .htaccess file by adding Header set Access-Control-Allow-Origin "*". In addition you might need to add Access-Control-Allow-Headers:*.

尝试向Access-Control-Allow-Origin:*向您提供 XML 的服务器端脚本添加标头。如果您不在 PHP(可以使用header())中执行此操作并尝试读取原始 XML 文件,则您可能必须通过在 .htaccess 文件中添加 .htaccess 文件来设置标头Header set Access-Control-Allow-Origin "*"。此外,您可能需要添加Access-Control-Allow-Headers:*.

Also I'd recommend to replace the * in production mode to disallow everybody from reading your data and instead add your own url there.

此外,我建议在生产模式中替换 * 以禁止所有人读取您的数据,而是在那里添加您自己的 url。

回答by Richard H

Without code impossible to say, but you could be running foul of the cross-site ajax limitation: you cannot make ajax requests to other domains.

没有代码是不可能的,但是您可能会违反跨站点 ajax 限制:您不能向其他域发出 ajax 请求。