javascriptajaxinternet-explorercross-domainxdr

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

Solution

  • Found out the issue last minute. In my case I needed to specify a timeout value, even though the requests were not timing out. In order to properly debug an XDR issue I'd suggest the following code, with each alert telling you what's going on with your code. As I said, in my case it was a missing timeout declaration. But the code below should debug any XDR problems:

           var xdr = new XDomainRequest();
                if (xdr) {
                    xdr.onerror = function () {
    //                    alert('xdr onerror');
                    };
                    xdr.ontimeout = function () {
    //                    alert('xdr ontimeout');
                    };
                    xdr.onprogress = function () {
    //                    alert("XDR onprogress");
    //                    alert("Got: " + xdr.responseText);
                    };
                    xdr.onload = function() {
    //                    alert('onload' + xdr.responseText);
                        callback(xdr.responseText);
                    };
                    xdr.timeout = 5000;
                    xdr.open("get", thisURL);
                    xdr.send();
                } else {
    //                alert('failed to create xdr');
                }