Error: “Access to restricted URI denied”

白昼怎懂夜的黑 提交于 2019-11-26 11:30:55

问题


Access to restricted URI denied\" code: \"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.

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

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


回答1:


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

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/MyProject and access it from http://localhost/index.html




回答2:


Sounds like you are breaking the same origin policy.

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




回答3:


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:*.

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




回答4:


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.



来源:https://stackoverflow.com/questions/6653825/error-access-to-restricted-uri-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!