47

I have an iframe using the jQuery 1.4.2 script. The same iframe is injected into both http and https sites. The jQuery script is included in the main HTML file as a relative path (e.g., /scripts/jquery-1.4.2.min.js).

When an AJAX call is made, Internet Explorer denies access. The AJAX is calling on another subdomain, but it's using the right protocol. All other browsers work but Internet Explorer gives the following error:

SCRIPT5: Access is denied.
jquery-1.4.2.min.js, line 127 character 344

I heard this error is from cross-domain AJAX calls. But why is IE the only one giving me crap? Is there an IE solution?

Also, this is my AJAX:

 $.ajax({
     url: thisURL,
     dataType: "json",
     data: {cmd : 'getMessage', uurl: urlVar, t: Math.random()},
     success: function(ret){
         callback(ret)
     }
 });
random
  • 9,774
  • 10
  • 66
  • 83
Kyle Cureau
  • 19,028
  • 23
  • 75
  • 104

10 Answers10

45

IE requires you to use XDomainRequest instead of XHR for cross site, you can try something like...

if ($.browser.msie && window.XDomainRequest) {
            // Use Microsoft XDR
            var xdr = new XDomainRequest();
            xdr.open("get", url);
            xdr.onload = function() {
                // XDomainRequest doesn't provide responseXml, so if you need it:
                var dom = new ActiveXObject("Microsoft.XMLDOM");
                dom.async = false;
                dom.loadXML(xdr.responseText);
            };
            xdr.send();
        } else {
            // your ajax request here
            $$.ajax({
                   url: thisURL,
                   dataType: "json",
                   data: {cmd : 'getMessage', uurl: urlVar, t: Math.random()},
                   success: function(ret){
                               callback(ret)
                    }
            });

        }

Reference

http://forum.jquery.com/topic/cross-domain-ajax-and-ie

not sure whether it fits your scenario

xdr = new XDomainRequest(); 
xdr.onload=function()
{
    alert(xdr.responseText);
}
xdr.open("GET", thisUrl); //thisURl ->your cross domain request URL 
//pass your data here
xdr.send([data]); 

you can find some more guidance here

Rafay
  • 30,950
  • 5
  • 68
  • 101
  • 1
    thanks for this solution and link! Unfortunately I don't know how to fit your code for my purpose. Could you help? I posted the ajax code above. Specifically I'm wondering under the browser.msie conditional, how I'd pass in my json params and how I'd call the callback with the return data when finished. Thanks a ton! – Kyle Cureau Feb 23 '11 at 06:25
  • How exactly do you send the data? Can you give an example with sending two parameters? – oneiros Feb 20 '12 at 21:14
  • @3nigma : How to do synchronous call in xdr request like how we do in ajax (example "async": flase ) – Hitesh Mar 18 '14 at 12:58
  • 1
    Great, in addition, for those who is working in php - server side code .. this is helpful ---- http://stackoverflow.com/questions/12025169/how-to-send-post-data-in-ie-cors-with-xdomainrequest – Nikhil Pingle Feb 16 '15 at 20:57
9

This solved the issue gracefully for me:

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

Just install/compile after jQuery and before your script and use the $.ajax method as you normally would, the rest is handled behind the automatically.

Kevinleary.net
  • 8,851
  • 3
  • 54
  • 46
  • 4
    I would like to add for anyone reading this: Make sure to use the same protocol as the originating call, i.e. HTTP or HTTPS. – Robert Brisita Jan 10 '14 at 02:19
4

Have you try to use the lastest of JQuery(> jquery-1.8.0)? Since the version 1.8.0, they solved some IE9's bugs. Perhaps this one too.

http://blog.jquery.com/2012/08/30/jquery-1-8-1-released/

matthieuR42
  • 194
  • 1
  • 3
2

I had a similar problem and the solution for me was to use jsonp instead of json. That way I didn't have to break out a customer version for IE.

You can only do this if the json server host supports the callback request variable or you have access to the server and can add support. Here is a page that helped me understand the process. Its .net mvc focused, but it gives a good over view of the diffrence between json and jsonp.

http://blogorama.nerdworks.in/entry-EnablingJSONPcallsonASPNETMVC.aspx

James Becwar
  • 1,176
  • 2
  • 11
  • 20
2

I was facing similar issue. I was using file upload control but it was hidden and I had another element trying to control the file upload and events to upload file in ajax way

try using the file upload control directly. this solved issue in my application.

iCoder
  • 41
  • 2
2

I get this bug (and thus google here) but the reason was very different. So if you don't have cross site and still get this access denied error: double check the value sent
let's say that you affect one of you variable with the bad following expression:

urlVar = $("theID").val // without () this was the error!

[...]ajax call:

data: {cmd : 'getMessage', uurl: urlVar, t: Math.random()},

Google/FF have no problem with this (check what is receive server side...) BUT IE refuse to send this!

Emmanuel Gleizer
  • 1,990
  • 16
  • 26
2

I changed my JQuery from version 1.10.1 to 1.10.2 and it seems to have solved this problem for me.

Trolloc
  • 21
  • 1
2

Check the domain you are accessing, following response headers should be there

"Access-Control-Allow-Methods" : "POST, GET, OPTIONS"
"Access-Control-Allow-Origin"  : "http://www.mydomain.com" or "*"

the other domain should allow your script request. One more header to be added to your response is P3P header.

"p3p" : "CP=IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"

it should help you out.

Simon
  • 33,714
  • 21
  • 133
  • 202
Furqan Hameedi
  • 4,372
  • 3
  • 27
  • 34
  • 1
    Having the same issue, and I activated my network capturing in IE9 and it doesn't even make a request to the server, so this probably won't solve it. – Eduard Luca Jun 04 '13 at 15:59
0

It seems that MS is finding its own way of doing things, rather than adopting industry recommendations. I found the solution here:
https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest/blob/master/jQuery.XDomainRequest.js

Jez D
  • 1,461
  • 2
  • 25
  • 52
0

Simply add 'callback=?' on your ajax URL request like here: http://wsvdmeer.blogspot.com.es/2012/08/bugfix-getjson-not-working-in-ie.html

AzuraStar
  • 31
  • 4