Javascript SCRIPT5:在 IE9 中对 xmlhttprequest 拒绝访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5793831/
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 18:52:53  来源:igfitidea点击:

SCRIPT5: Access is denied in IE9 on xmlhttprequest

javascriptxmlhttprequestinternet-explorer-9

提问by Eric L

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

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

F12弹回:SCRIPT5:访问被拒绝。在第 95 行,即 xhttp.open 行。

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

我的 JavaScript 看起来格式良好,Firefox 做了我认为应该做的事情。

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?

我已经阅读了很多与此非常相似的问题,因此我查看了同源策略,但考虑到 foo.xml 与 html 文件位于同一目录中,我看不出它是如何应用的。我在本地 Intranet 上打开了脚本权限,并告诉 McAfee 休息五分钟,以防万一。我什至尝试以管理员身份运行 IE,所以这真的不是权限问题吗?为什么 IE 会被拒绝访问本地文件?

回答by Philips

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.

此示例说明如何使用 AJAX 从任何网站拉取资源。它可以跨浏览器工作。我已经在 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;

回答by Walid Ibrahim

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.

在 IE7、IE8 和 IE9 上,只需转到“设置”->“Internet 选项”->“安全”->“自定义级别”,然后将“其他”下的安全设置更改为“跨域访问数据源”设置为启用。

回答by Rockwell1960

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.

如果找不到 .replace 方法的目标页面(我输入了错误的页面名称),也会遇到此错误消息(SCRIPT5:访问被拒绝。)。我知道,因为它刚刚发生在我身上,这就是为什么我去搜索有关错误消息含义的更多信息。

回答by Brenden

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

很可能,您需要通过 SSL 提供 Javascript。

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

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

回答by gautam

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

我在 IE10 上遇到过类似的问题。我通过使用 jQuery ajax 请求来检索数据有一个解决方法:

$.ajax({
    url: YOUR_XML_FILE
    aync: false,
    success: function (data) {   
        // Store data into a variable
    },
    dataType: YOUR_DATA_TYPE,
    complete: ON_COMPLETE_FUNCTION_CALL
});

回答by Orhun Alp Oral

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:

可能您正在请求外部资源,这种情况下 IE 需要 XDomain 对象。查看下面的示例代码,了解如何为所有跨域浏览器发出ajax请求:

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);
}

回答by Nate Koppenhaver

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.

我认为问题在于该文件在您的本地计算机上,而 IE 拒绝访问,因为如果它允许脚本访问运行浏览器的 comp 上的文件,那将是一个巨大的安全漏洞。
如果您可以访问可以用作一个服务器或其他组合的服务器,也许您可​​以尝试将文件放在该服务器上,然后像从网站上一样运行脚本。

回答by Ranjit Kumar

  $.ajax({
        url: '//freegeoip.net/json/',
        type: 'POST',
        dataType: 'jsonp',
        success: function(location) {
            alert(location.ip);
        }
    });

This code will work https sites too

此代码也适用于 https 站点