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

给你一囗甜甜゛ 提交于 2019-12-01 18:16:03

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/16610720/ajax-works-if-chrome-dev-tools-open-but-not-if-chrome-web-tools-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!