Strange and unexpected behaviour of $.ajax, error and localhost

Strange and unexpected behaviour of $.ajax, error and localhost

Hello,
I found this behaviour of $.ajax using this simple code. I tested this on Firefox, Chrome and Safari (not on IE :| ). I'm using jQuery 1.4.2

Suppose you have this code:

  1. function test() {
  2. $.ajax( {
  3. url : 'test/GameConfiguration.json',
  4. dataType : 'json',
  5. data : {
  6. a : 'aaa'
  7. },
  8. cache : false,
  9. method : 'get',
  10. timeout : 10000, //10 secs of timeout 
  11. success : function(data, textStatus, XMLHttpRequest) {
  12. console.log("success");
  13. if (data == null)
  14. console.log("it's not a real success");
  15. },
  16. error : function(XMLHttpRequest, textStatus, errorThrown) {
  17. console.log("error: " + textStatus);
  18. }
  19. });
  20. }
What this function does is pretty obvious. I created a page with a button that has onclick="test()", but I found this bahaviour even in other situations. 

Here is the problem: I load the page from an Apache webserver on localhost (a standard Wamp install). Then before clicking the button I shut the server off. When I click the button that fires test() it happens that:

  • The request doesn't take 10 seconds to complete, but fires early
  • The success handler will be called, but the data argument is set to null.
I tried: disabling caches (using firebug for example). No effect, it doesn't work the expected way. 

Anyway if I use my collegue PC to do the test and I do it this way:

  1. I load the page on his computer from my webserver
  2. Before calling test I DISCONNECT my computer from the network
  3. I press the button that fires test()
This way the timeout takes 10 seconds and error is correctly fired and textStatus is set to timeout. 

I'm happy that this works as expected on real world conditions, but I'd like to test a more complex functionality locally (ajax apps are not always this simple :))

At least I thinks you should document this behaviour on the API reference.

Cheers