Ajax request “Access is denied” in IE

2020-07-10 06:13发布

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

$.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.

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

Why am I getting this error?

5条回答
来,给爷笑一个
2楼-- · 2020-07-10 06:46

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

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);
查看更多
一夜七次
3楼-- · 2020-07-10 06:46

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.

查看更多
家丑人穷心不美
4楼-- · 2020-07-10 06:56

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

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

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

查看更多
我命由我不由天
5楼-- · 2020-07-10 06:57

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.

<meta http-equiv="X-UA-Compatible" content="IE=Edge" >
查看更多
你好瞎i
6楼-- · 2020-07-10 06:57

Quoting "epascarello" from an other very similar question :

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.

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.

The page that the Ajax call is made from "www.example.com" and is calling "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

<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 !

查看更多
登录 后发表回答