Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX works if chrome dev tools open but not if chrome web tools closed?

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?

like image 548
Rob Johnstone Avatar asked Oct 22 '22 09:10

Rob Johnstone


1 Answers

@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!)

like image 50
Rob Johnstone Avatar answered Nov 09 '22 04:11

Rob Johnstone