0

I'm performing a cross domain get request using similar to the code below (a very simplified version).

var addData = function() {
    $.ajax({
      type:'GET',
      url:'http://DifferentDomain.com/data.php',
      data:'body',
      success: function(data){
        $('body').html(data);
      }
    });
}

Now as IE doesn't support CORS I've needed to use an XDomainRequest using the iecors plugin which works great, as when IE fails with a XHttpRequest it then switches over to XDR.

However the issue I'm having is with $('body').html(data); This only seems to work in IE once I execute it live in the console, and not when it is first called on $(document).ready? (This works fine in all other browsers as they support XHR)

I think this is an issue with timing so I'm not really sure how to get around this, do I use a setTimeout() and run it twice for IE browsers?. Please can someone offer some advice? Many thanks in advance.

EDIT

I've found a workaround by executing it with a setTimeout , but still feel this isn't the best solution, e.g.

 setTimeout(function(){
   addData();
 },0);
apsillers
  • 112,806
  • 17
  • 235
  • 239
dev
  • 3,969
  • 3
  • 24
  • 36
  • When do you call this? You say "on load up", but what does that mean? – Ian Jan 28 '13 at 14:50
  • Sorry this is called by `$(document).ready`. I'll edit my question to show this. – dev Jan 28 '13 at 14:53
  • Ahh okay. And have you tried using the `error` option for the `$.ajax` call to see if any HTTP errors arise? Also, have you checked IE's Developer Tools for any errors? – Ian Jan 28 '13 at 14:59
  • I've looked and they both don't report any errors. When I run it live the second time all works fine for IE, so I don't think there is any issues with the code or accessing the data file. – dev Jan 28 '13 at 15:07
  • Does it make a difference if you switch it to onload instead of ready? – epascarello Jan 28 '13 at 15:11
  • No changing this to load doesn't seem to help :( I've just tried executed it initially for all browsers and then setting a timeout of one second and executing it again for just IE browsers and this has worked. Though I feel in my gut this isn't the best workaround . – dev Jan 28 '13 at 15:20
  • Just found I don't even need to execute it twice or with a long time ...just doing it the once with a timeout of 0 seems to work. Please see above in my edit. – dev Jan 28 '13 at 15:41

1 Answers1

0

I've found a workaround by executing it with a setTimeout , but still feel this isn't the best solution, e.g.

 setTimeout(function(){
   addData();
 },0);
dev
  • 3,969
  • 3
  • 24
  • 36
  • setTimeout doesn't do anything. I did quite a bit of testing, and discovered that the real issue is that XDomainRequest is being garbage collected. With the JQuery XDR AJAX transport you're using, the XDomainRequest object will go out of scope. Depending on your luck, if the IE8 garbage collector runs before the request completes you'll see it vanish with the request going to "Aborted" in the network trace. I have a detailed description of the problem with workarounds here: http://stackoverflow.com/questions/8058446/ie-xdomainrequest-not-always-work – ShadowChaser Dec 20 '14 at 19:03