简体   繁体   English

Chrome正在缓存HTTP PUT请求

[英]Chrome is caching an HTTP PUT request

I have this bizarre issue with Chrome. 我对Chrome有这个奇怪的问题。 It quite often appears to cache PUT requests. 它经常出现缓存PUT请求。

The Details: I have an app using backbone.js and when trying to persist some changes to a model (backbone automatically generates a PUT request), Chrome just wont send that request to the server. 详细信息:我有一个使用backbone.js的应用程序,当尝试将某些更改保留到模型时(主干自动生成PUT请求),Chrome只是不会将该请求发送到服务器。 It works perfectly fine in Firefox and IE (haven't seen the issue in Safari so far). 它在Firefox和IE中运行得非常好(到目前为止还没有在Safari中看到过这个问题)。

Here's a screenshot from the Chrome developer tools' Network tab. 以下是Chrome开发者工具“网络”标签的屏幕截图。 As you can see, the response for the PUT request is being returned from cache (the request doesn't hit the server!!) 如您所见,PUT请求的响应是从缓存返回的(请求没有到达服务器!!) Chrome缓存PUT请求

Here's a screenshot of the header details of that same request. 这是同一请求的标题详细信息的屏幕截图。 Once again, it's evident that Chrome doesn't bother sending the PUT request to the server. 再一次,很明显Chrome并不打算将PUT请求发送到服务器。 Chrome缓存了PUT请求标头

The payload of the request is JSON data. 请求的有效负载是JSON数据。 Any thoughts as to why this is happening / what I'm doing wrong? 有关为什么会发生这种情况的想法/我做错了什么?

UPDATE: Chromium has confirmed that this is indeed a bug on it's end (thanks Jan Hančič). 更新: Chromium已经确认这确实是一个错误 (感谢JanHančič)。

TEMPORARY SOLUTION I ended up overriding Backbone.sync method and appending a timestamp to the querystring of PUT, POST and DELETE requests so that they are always unique: 临时解决方案我最终覆盖了Backbone.sync方法并将时间戳附加到PUT,POST和DELETE请求的查询字符串,以便它们始终是唯一的:

if(!options.data && model && (method == 'create' || method == 'update' || method == 'delete')) {
    params.url += (params.url.indexOf('?') == -1 ? '?' : '&') + '_=' + new Date().getTime();
}

I use extra parameter to avoid caching: 我使用额外的参数来避免缓存:

url += '?_dc=' + Math.random().toFixed(20).replace('.', '');

I don't interpret this parameter on server side. 我不解释服务器端的这个参数。

EDIT: Besides of chrome there are a lot things could cache requests - user's proxy server for instance. 编辑:除了chrome之外,还有很多东西可以缓存请求 - 例如用户的代理服务器。 I think additional query parameter is a good solution to keep out of caching. 我认为额外的查询参数是一个很好的解决方案,以防止缓存。

Backbone use jQuery or Zepto to make the AJAX request. Backbone使用jQuery或Zepto来发出AJAX请求。 Assuming that you are using jQuery, set the cache off. 假设您正在使用jQuery,请关闭缓存。

Run this to set the cache off in the overall application so you will not need to worry about cache: 运行此命令可在整个应用程序中关闭缓存,因此您无需担心缓存:

$.ajaxSetup({
      cache : false
});

If keep the cache on is important for your business, I think that you could do something like this for specific no cache calls: 如果保持缓存对您的业务很重要,我认为您可以针对特定的无缓存调用执行类似的操作:

model.save({}, {cache:false});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM