5

I'm loading a json file via ajax. If Chrome dev tools is open, everything functions perfectly. If Chrome dev tools is closed it fails. Thankfully dev tools still keeps doing it's thing even when closed so I can still see the exception I get:

Failed to load resource: the server responded with a status of 412 (Precondition Failed) http://localhost/experiments/escape/maps/test.json

Why would there be a precondition on whether dev tools is open? Also, it seems unlikely that opening and closing the dev tools could in any way affect the server's behaviour so I think it is Chrome that is preventing the request rather than the server as suggested in the exception.

Unfortunately, dev tools does not keep track of network activity when closed so I can't use the network tab to get any further info.

The AJAX is handled via JQuery with the following code:

map.load = function(mapName, tileSource, tileWidth, tileHeight, onLoad) {
    $.ajax({
        url: '../escape/maps/'+mapName+'.json',
        type: 'post',
        success: function(mapData) {
            // there's loads of stuff in here but I don't think it's relevant to the question as the failure prevents the success method from being called.
        }
    });
};

This code causes no issues in Firefox and so does seem specifically to be connected to Chrome Dev Tools. Any suggestions welcome as I'm completely flummoxed!

EDIT: Ok so it's not dev tools fault at all - I had disabled the cache in dev tools, re-enabling it allows the script to work correctly. Why does my code depend on the cache? Disabling / enabling the cache in Firefox does not cause any issues

EDIT2: Ok, I think I'm getting close. The precondition that is failing is the if-modified-since condition (the file hasn't changed). I assume that chrome is sending this to confirm whether or not to use the cached version, however, despite the precondition failing it does not load the cached version. I thought this might mean the cache was corrupted in some way so I cleared the cache. Unfortunately this doesn't solve the issue. The file will happily load once but on the next time I'm back where I started with the same issue. Any ideas?

Rob Johnstone
  • 1,704
  • 9
  • 14
  • Are you explicitly passing `412` from your server. If you are you might get some hint from looking at your server code, exactly what precondition is failing? Try debugging your server code. – Juzer Ali May 17 '13 at 13:57
  • It's loading a static file so there is no server code in this case. I'm fairly sure it's nothing to do with the server as otherwise Firefox would experience the same issues. – Rob Johnstone May 17 '13 at 14:00
  • Its not that the problem is at server. How server is handling it might give some cues as to what chrome dev tools is doing. Maybe it is sending some header which your framework (if you are using one) is not playing well with. – Juzer Ali May 17 '13 at 14:02
  • 5
    @RobJ Try explicitly setting `cache:false` in your ajax method to see if that changes the behavior. It shouldn't matter for a Post though – Rondel May 17 '13 at 14:14

1 Answers1

3

@Rondel - You've got it! The issue was that I was stupidly using 'post' to fetch a static file. Post requests are never supposed to be cached so that is why Chrome doesn't retrieve it. I've still got no idea why chrome still sends the if-modified-since header but in any case changing the request type to get is the solution to the problem (Sorry Crome Dev tools for unfairly blaming you - the issue, as usual, was my code!)

Rob Johnstone
  • 1,704
  • 9
  • 14