Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent ajax (XDR) response from IE

I'm making an ajax request from an iframe that is injected onto every page via an IE plugin. I'm using IE's cross domain request because jQuery's ajax fails for IE. This works 75% of the time on IE8 & 9. The other 25%, the xdr.onload doesn't even fire.

The server php is doing its job...the log looks identical for when onload does and does not fire. Also, xdr.onerror doesn't fire either.

Any ideas?

        thisURL = "http://example.com/getmsg.php?cmd=getMessage&iid=ddeb2c1228&uurl=http%3A%2F%2Fwww.cnn.com%2F&t=" + Math.random(); 

        // Use Microsoft XDR
        var xdr = new XDomainRequest();
        xdr.open("GET", thisURL);
        xdr.onload = function() {
            // this is sometimes called, sometimes not in IE
            alert('INCONSISTENT ALERT');
            callback(xdr.responseText);
        };
        xdr.send();
like image 481
Kyle Cureau Avatar asked Mar 09 '11 18:03

Kyle Cureau


1 Answers

I was having a very similar problem: XDomainRequests failing only some of the time, despite Fiddler showing all request and response headers being sent exactly as I intended. I had defined every event handler and the timeout property on these XDR objects, and none of the handlers were being called. The F12 developer tools showed the requests as aborted. Both GETs and POSTs could be aborted, to multiple different domains, but the first request always worked successfully.

Then I tried putting the calls to xdr.send in a timeout, like this:

setTimeout(function () {
    xdr.send();
}, 0);

and it worked. I have no idea why, but maybe this will be helpful to someone else.

like image 157
sethobrien Avatar answered Sep 22 '22 14:09

sethobrien