如果chrome开发工具可以使用AJAX,但如果chrome web工具关闭,AJAX可以使用吗? [英] AJAX works if chrome dev tools open but not if chrome web tools closed?

查看:148
本文介绍了如果chrome开发工具可以使用AJAX,但如果chrome web工具关闭,AJAX可以使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过ajax加载了一个json文件。如果Chrome开发工具是开放的,那么所有功能都可以完美实现如果Chrome开发工具关闭,则失败。幸运的是,开发工具仍然继续做它的事情,即使关闭,所以我仍然可以看到我得到的异常:

 加载资源失败:服务器响应状态为412(Precondition Failed)http://localhost/experiments/escape/maps/test.json 

为什么会存在开发工具是否开放的先决条件?此外,打开和关闭开发工具似乎不太可能以任何方式影响服务器的行为,所以我认为这是Chrome阻止请求,而不是异常中建议的服务器。



不幸的是,开发工具在关闭时没有跟踪网络活动,所以我无法使用网络标签获取更多信息。



AJAX通过JQuery使用以下代码处理:

  map.load = function(mapName,tileSource,tileWidth,tileHeight,onLoad) {
$ .ajax({
url:'../escape/maps/'+mapName+'.json',
type:'post',
success:function( mapData){
//这里有大量的东西,但是我认为它与这个问题没有关系,因为失败阻止了成功方法被调用。
}
});
};

这段代码在Firefox中不会引起任何问题,因此似乎专门连接到Chrome开发工具。任何建议欢迎,因为我完全flummoxed!

编辑:好,所以它不是开发工具的错误 - 我已经禁用开发工具中的缓存,重新启用它允许脚本正常工作。为什么我的代码依赖于缓存?在Firefox中禁用/启用缓存不会导致任何问题。



编辑2:好吧,我想我正在接近。失败的先决条件是if-modified-since条件(文件没有改变)。我认为chrome发送这个来确认是否使用缓存版本,但是,尽管前提条件失败,它不会加载缓存版本。我认为这可能意味着缓存已被破坏,所以我清除了缓存。不幸的是,这并不能解决问题。该文件将愉快地加载一次,但在下次我回到我开始处理同一问题时。有任何想法吗?

解决方案

@Rondel - 你懂了!问题在于我愚蠢地使用'post'来获取静态文件。发布请求永远不会被缓存,因此Chrome不会检索它。我仍然不知道为什么chrome仍然发送if-modified-since标题,但无论如何改变请求类型以获得问题的解决方案(对不起,Crome开发工具不公正地指责你 - 问题一如既往,是我的代码!)


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?

解决方案

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

这篇关于如果chrome开发工具可以使用AJAX,但如果chrome web工具关闭,AJAX可以使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆