简体   繁体   中英

Ajax request “Access is denied” in IE

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?

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

--- 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, ie HTTP or HTTPS.

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 !

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM