Blog Archive

Tuesday, September 10, 2013

Chrome, Safari and duplicate XHR Requests

Today I ran across a really weird problem in an angular project I'm currently working on.

The page performed two XHR Requests (well, angular did but that doesn't matter) to the same URL only milliseconds appart.

Now this is what happens in Firefox (Nightly 23.0a1): Both requests go to the server and return the same result (an error but this makes no difference).

Now this is what happens in Chrome (31.0.1622.0) and Safari (6.0.5): Only one of the request hits the wire and therefore only one of the requests hits the server. But both requests trigger the xhr.onreadystatechange Events.

To try it out paste the following code into your console
var xhr = new XMLHttpRequest(); xhr.onreadystatechange=function() {console.log('xhr1-change');};'get', 'http://localhost:8989/', true); xhr.send(); var xhr2 = new XMLHttpRequest(); xhr2.onreadystatechange=function() {console.log('xhr2-change');};'get', 'http://localhost:8989/', true); xhr2.send();

To be honest: I have no idea why this happens. I assume it is some sort of caching but it is a really big gotcha when debugging... Hope this saves you some hours...

No comments:

Post a Comment