47
var xhttp=new XMLHttpRequest();
xhttp.open('GET', 'foo.xml', false);

F12 pops back: SCRIPT5: Access is denied. on Line 95, which is the xhttp.open line.

My JavaScript seems well-formed, and Firefox does what I think it should.

I've read a lot of questions very similar to this one, so I've checked out the Same Origin Policy, but I can't see how it'd apply considering foo.xml is in the same directory as the html file. I opened up the scripting permissions on my local intranet, and told McAfee to take a five-minute break, just to be sure. I even tried running IE as admin, so this can't really be a permissions issue can it? Why else would IE be denied access to a local file?

ndsmyter
  • 6,535
  • 3
  • 22
  • 37
Eric L
  • 471
  • 1
  • 4
  • 3

11 Answers11

5

This example illustrate how to use AJAX to pull resourcess from any website. it works across browsers. i have tested it on IE8-IE10, safari, chrome, firefox, opera.

if (window.XDomainRequest) xmlhttp = new XDomainRequest();
else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.open("GET", "http://api.hostip.info/get_html.php", false);
xmlhttp.send();

hostipInfo = xmlhttp.responseText.split("\n");
var IP = false;
for (i = 0; hostipInfo.length >= i; i++) {
    if (hostipInfo[i]) {

        ipAddress = hostipInfo[i].split(":");
        if (ipAddress[0] == "IP") {
            IP = ipAddress[1];
        }
    }
}
return IP;
ndsmyter
  • 6,535
  • 3
  • 22
  • 37
Philips
  • 71
  • 1
  • 3
5

You likely have a Mark-of-the-Web on the local file. See http://blogs.msdn.com/b/ieinternals/archive/2011/03/23/understanding-local-machine-zone-lockdown-restricted-this-webpage-from-running-scripts-or-activex-controls.aspx for an explanation.

EricLaw
  • 56,563
  • 7
  • 151
  • 196
  • 1
    Thank you for the explanation of MOTW. My XML file doesn't have one, and I even added one to see if I could get the error message to change. No such luck. – Eric L Apr 27 '11 at 04:06
4

On IE7, IE8, and IE9 just go to Settings->Internet Options->Security->Custom Level and change security settings under "Miscellaneous" set "Access data sources across domains" to Enable.

Walid Ibrahim
  • 220
  • 2
  • 2
  • 6
    @Skurpi Agree, well that's the price of using IE. With our application we stated that only Firefox and Chrome are supported. In case our clients wanted to use IE, then they need to make sure this setting is enabled, willingly, our IE clients changed their settings. – Walid Ibrahim Feb 08 '13 at 16:32
  • When attempting to run XMLHTTPRequest from a bookmarklet, I kept getting this access denied error. When I ran the script on a page, it ran fine. This was the only solution that worked. Win 7 IE 9. – thefoyer Feb 12 '13 at 17:18
3

This error message (SCRIPT5: Access is denied.) can also be encountered if the target page of a .replace method is not found (I had entered the page name incorrectly). I know because it just happened to me, which is why I went searching for some more information about the meaning of the error message.

  • 1
    What do you mean by "replace method"? I have this error message, but I couldn't figure out what caused it. – Cat Chen May 21 '13 at 16:48
1

Most likely, you need to have the Javascript served over SSL.

Source: https://www.parse.com/questions/internet-explorer-and-the-javascript-sdk

Brenden
  • 7,708
  • 11
  • 61
  • 75
0

Probably you are requesting for an external resource, this case IE needs the XDomain object. See the sample code below for how to make ajax request for all browsers with cross domains:

Tork.post = function (url,data,callBack,callBackParameter){
    if (url.indexOf("?")>0){
        data = url.substring(url.indexOf("?")+1)+"&"+ data;
        url = url.substring(0,url.indexOf("?"));
    }
    data += "&randomNumberG=" + Math.random() + (Tork.debug?"&debug=1":"");
    var xmlhttp;
    if (window.XDomainRequest)
    {
        xmlhttp=new XDomainRequest();
        xmlhttp.onload = function(){callBack(xmlhttp.responseText)};
    }
    else if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            Tork.msg("Response:"+xmlhttp.responseText);
            callBack(xmlhttp.responseText,callBackParameter);
            Tork.showLoadingScreen(false);
        }
    }
    xmlhttp.open("POST",Tork.baseURL+url,true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(data);
}
Orhun Alp Oral
  • 734
  • 1
  • 7
  • 14
0

I had faced similar issue on IE10. I had a workaround by using the jQuery ajax request to retrieve data:

$.ajax({
    url: YOUR_XML_FILE
    aync: false,
    success: function (data) {   
        // Store data into a variable
    },
    dataType: YOUR_DATA_TYPE,
    complete: ON_COMPLETE_FUNCTION_CALL
});
ndsmyter
  • 6,535
  • 3
  • 22
  • 37
gautam
  • 1
  • 1
0

I think that the issue is that the file is on your local computer, and IE is denying access because if it let scripts have access to files on the comp that the browser is running on, that would be a HUGE security hole.
If you have access to a server or another comp that you could use as one, maybe you could try putting the files on the that, and then running the scripts as you would from a website.

Nate Koppenhaver
  • 1,676
  • 3
  • 21
  • 31
  • Oddly, if this is true, IE has a large security hole -- I have discovered I can load and manipulate this file through declaring an XML data island and using datafld inline. Not that I love this idea, but it's the best I've got right now. – Eric L Apr 27 '11 at 04:12
-1
  $.ajax({
        url: '//freegeoip.net/json/',
        type: 'POST',
        dataType: 'jsonp',
        success: function(location) {
            alert(location.ip);
        }
    });

This code will work https sites too

Ranjit Kumar
  • 273
  • 3
  • 16
-3

Open the Internet Explorer Developer Tool, Tools -> F12 developer tools. (I think you can also press F12 to get it)

Change the Document Mode to Standards. (The page should be automatically refresh, if you change the Document Mode)

Problem should be fixed. Enjoy

Shuliyey
  • 415
  • 4
  • 7