Ajax Inconsistent Response (XDR) from IE

I am making an ajax request from an iframe that is injected into each page through an IE plugin. I am using IE cross-domain query since jQuery ajax does not work for IE. This works 75% of the time on IE8 and 9. The remaining 25%, xdr.onload do not even work.

The php server does its job ... the log looks identical when onload executes and does not start. In addition, xdr.onerror does not xdr.onerror .

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(); 
+6
javascript internet-explorer ajax cross-domain xdr
source share
5 answers

Found a problem at the last minute. In my case, I needed to specify a timeout value, even if the requests were not synchronized. To properly debug the XDR problem, I suggest the following code: each warning tells you what is happening with your code. As I said, in my case this is 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'); } 
+4
source share

I had a very similar problem: XDomainRequests did not run for only a while, even though Fiddler showed that all request and response headers were sent exactly as I expected. I defined each event handler and timeout property on these XDR objects, and none of the handlers were called. F12 developer tools showed that requests were aborted. Both GET and POST can be interrupted for several different domains, but the first request always worked successfully.

Then I tried to put xdr.send calls in a timeout, for example:

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

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

+9
source share

You entered the name correctly here. The experience is quite controversial. Let it go through him ...

 var xdr, err, res, foo, url; xdr = new XDomainRequest(); err = function(){ alert("There was an error, operation aborted"); } res = function(){ alert("Success! " + xdr.responseText); } foo = function(){ return; } url = "http://hello.com/here/is/the/url/?query=true&whatever=asd"; xdr.onerror = err; xdr.ontimeout = foo; xdr.onprogress = foo; xdr.onload = res; xdr.timeout = 5000; xdr.open("get", url); xdr.send(null); 

The XDomainRequest object is handled differently in each IE.

In IE9 โ†’, the XDomainRequest object requires that all descriptors be given a method. A value that treats as onerror, onload, ontimeout, and onprogress is all something you need to do. Without defining a method for these descriptors, you will receive a network response "operation aborted."

In IE7 / 8/9 โ†’ XDomainRequest, the default is ASYNC. It will execute the code further down the stack, regardless of whether the xdr object terminated or not. Setting setTimeout may be a solution, but it should not.

In this case, activate the event and listen to the event before executing any additional code. An example of this might be (in jquery) ...

 // call this method in xdr onload $(document).trigger("xdr_complete"); // use this wrapper in code you want to execute after the complete of xdr $(document).bind("xdr_complete", function(){ ... }); 

In IE7 / IE8 you will notice that it works. IE7 and IE8 are pretty "free" in the sense that they are not interrupted when there is no method for descriptors.

+7
source share

It is hard to give you the final answer here.

First you should try to use Fiddler to find out how you get any kind of network failure. This will help you understand whether the request was actually made and the response from the server.

Secondly, and I do not know if this is applicable, we recently encountered the cross-domain problem in IE. The code worked perfectly in other browsers - Firefox, Safari and Chrome. The problem in this case was that the request returned 404, which was expected. Despite this, IE stopped all execution at this point, even our error handling in event 404 never started. So you can get some of them here. Firefox, Safari etc. Allow the script to continue. If you return an error, you may want to return it with an error.

0
source share
  • First, add an onerror handler to debug your stuff. Write them down and see what happens.
  • Secondly, your "math.random" approach is erroneous. A better option is to use a new Date (). GetTime (), this will provide you unique requests over time.
  • Third, you could (probably should) use POST methods to bypass IE, obviously based on standards (;-))
0
source share

All Articles