10

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?

Community
  • 1
  • 1
Bala
  • 390
  • 3
  • 8
  • 25
  • 1
    You haven't posted any code so it's tricky to say what might be the cause. What is the URL where you are executing the ajax and what is the URL you're fetching using ajax? – pimvdb Jul 11 '11 at 17:14
  • 1
    Personally I would try searching for that error message on StackOverflow and possibly Google. – James Jul 11 '11 at 17:17
  • I also have another javascript doing calling the same function to do some work. could that be the cause? – Bala Jul 11 '11 at 17:29
  • @user749069 look at the help to learn how to post code, it is not `` tags – epascarello Jul 11 '11 at 17:46
  • You are missing the most important details. What is `xml_file` and what does the url of the page look like? – epascarello Jul 11 '11 at 17:47
  • im passing projects.xml which is present in the same directory. so im sending ./projects.xml – Bala Jul 12 '11 at 08:00

4 Answers4

21

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

Kevin B
  • 94,570
  • 16
  • 163
  • 180
13

Sounds like you are breaking the same origin policy.

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

epascarello
  • 204,599
  • 20
  • 195
  • 236
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.

zatatatata
  • 4,761
  • 1
  • 20
  • 41
  • >"Also I'd recommend to replace the * in production mode to disable everybody from reading your data and instead add your own url there." Note, that this is purely optional for clients and only browsers respect this. It does not prevent anyone from reading your data. – StanE Nov 18 '16 at 04:17
1

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.

Richard H
  • 38,037
  • 37
  • 111
  • 138