javascript IE 中的 Ajax 请求“访问被拒绝”

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

Ajax request "Access is denied" in IE

javascriptajaxjquery

提问by user1874941

I use ajax request in order to check response of websites as follow,

我使用 ajax 请求来检查网站的响应如下,

$.ajax ({
    url: 'https://www.example.com',
    cache: false,
    success : function() {
        alert(new Date() - start)               
    }, 
}) 

It works on my local pc in all browsers. When I put it on the server, it works in Chrome and Firefox but not in IE8.

它适用于我本地电脑的所有浏览器。当我把它放在服务器上时,它可以在 Chrome 和 Firefox 中运行,但不能在 IE8 中运行。

I get the error: "Access is denied" jquery.min.js

我收到错误: "Access is denied" jquery.min.js

Why am I getting this error?

为什么我收到这个错误?

采纳答案by user1874941

For my case the problem is resulted because of compatibility mode. I am in intranet and internet explorer is running with compatibility mode. I added following tag and this solved all my problems. It forces IE to not use compatibility mode.

对于我的情况,问题是由于兼容模式导致的。我在 Intranet 中,Internet Explorer 正在以兼容模式运行。我添加了以下标签,这解决了我所有的问题。它强制 IE 不使用兼容模式。

<meta http-equiv="X-UA-Compatible" content="IE=Edge" >

回答by Robert Brisita

--- JAN 2014 ---

IE8 and IE9 use a different method (XDomainRequest) to communicate with cross domains. You should consider using this if they are using jQuery:

--- JAN 2014 ---

IE8 和 IE9 使用不同的方法 (XDomainRequest) 与跨域通信。如果他们使用 jQuery,您应该考虑使用它:

https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

Make sure to use the same protocol as the originating call, i.e. HTTP or HTTPS.

确保使用与发起呼叫相同的协议,即 HTTP 或 HTTPS。

回答by BastienSander

Quoting "epascarello" from an other very similar question :

从另一个非常相似的问题中引用“epascarello”:

Making a call to a sub domain is seen as a different domain because of the Same Origin policy. Make sure that you are setting document.domain to avoid access denied with the Same Origin policy.

由于同源策略,调用子域被视为不同的域。确保您正在设置 document.domain 以避免访问被同源策略拒绝。

To get the document.domain in sync you need to set it in two places. Add a script tag that set the domain, and you need to have an iframe on the page that sets the same thing on the other domain.

要使 document.domain 同步,您需要在两个地方设置它。添加一个设置域的脚本标记,并且您需要在页面上有一个 iframe 来在另一个域上设置相同的内容。

The page that the Ajax call is made from "www.example.com" and is calling "ajax.example.com":

Ajax 调用的页面来自“www.example.com”并调用“ajax.example.com”:

<script type="text/javascript">
    document.domain = "example.com";
</script>
<iframe src="http://ajax.example.com/domainCode.html"></iframe>

The "domainCode.html" would just contain the script tag

“domainCode.html”将只包含脚本标签

<html>
    <head>
        <script type="text/javascript">
            document.domain = "example.com";
        </script>
    </head>
<body>
</body>
</html>

With that in place you should be able to talk between your sub domains.

有了这个,您应该能够在您的子域之间进行交谈。

Hope that helps !

希望有帮助!

回答by ali bagheri

Note -- Note do not use "http://www.domain.xxx" for URL in ajax. only use path(directory) and page name without address.

注意——注意不要在 ajax 中使用“ http://www.domain.xxx”作为 URL。只使用路径(目录)和页面名称而不使用地址。

false state:

错误状态:

var AJAXobj = createAjax();
AJAXobj.onreadystatechange = handlesAJAXcheck;
AJAXobj.open('POST', 'http://www.example.com/dir/getSecurityCode.php', true);
AJAXobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
AJAXobj.send(pack);

true state:

真实状态:

var AJAXobj = createAjax();
AJAXobj.onreadystatechange = handlesAJAXcheck;
AJAXobj.open('POST', 'dir/getSecurityCode.php', true);   // <<--- note
AJAXobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
AJAXobj.send(pack);

回答by GrokSrc

I had this problem in IE8. What solved it for me was changing my ajax request to use the same protocol as the original page request. In my case the original page was requested over https and the ajax request was using http. Switching them both to use https fixed the problem.

我在 IE8 中遇到了这个问题。为我解决的是将我的 ajax 请求更改为使用与原始页面请求相同的协议。在我的情况下,原始页面是通过 https 请求的,而 ajax 请求使用的是 http。将它们都切换为使用 https 解决了问题。