"再打开上次关闭的标签"使显示最新的Ajax请求的内容再打、标签、内容、最新

2023-09-11 00:40:23 作者:违心话太多丶

我使用的HTML 5历史的API来保存状态时,Ajax请求发生,我提供完整的HTML内容,如果用户请求相同的页面,没有Ajax请求。

I am using HTML 5 history api to save state when ajax requests happen and i provide full html content if user request to same page with none ajax request.

浏览器重新打开上次关闭的标签功能实现了,没有击中服务器最后Ajax请求的内容。如果浏览器将要求没有带来最后的请求内容,那么一切都将工作没有问题。但是,浏览器只显示最后一个Ajax请求的内容。

"Re-open last closed tab" feature of browser brings last ajax request content without hitting to server. If browser would request without bring last request content then everything would work without problem. But browser just show last ajax request content.

我已经经历过这样的在Chrome 17,火狐10(因为它不支持历史API我还没有尝试过在IE9)

I have been experienced this on Chrome 17, Firefox 10. (i haven't tried it on ie9 because it has no support history api)

什么是众所周知的,这个问题的解决?

What is well-known solution for this problem ?

编辑:这些Ajax请求仅仅是取请求到服务器

These ajax requests are just "get" request to server.

实在是无法证明它jsfiddle.net因为几方面的原因。你能证明它在你的本地主机如下图所示。

it is really not possible to demonstrate it in jsfiddle.net because few reasons. You can demonstrate it in your localhost like below.

请取请求服务器拉JSON对象,然后按该URL成为历史API像下面。

Make "get" request to server and pull json objects then push that url into history api like below.

history.pushState(null,null,url);

然后关闭该选项卡,并单击浏览器的重新打开上次关闭的标签功能。你看到了什么 ? JSON响应的身体?浏览器显示它未做要求的服务器,对不对?

Then close that tab and click "Re-open last closed tab" feature of your browser. What do you see ? Json response body ? Browser shows it without making request to server, right ?

推荐答案

问题是导致通过HTTP响应头。头被包含缓存信息的Ajax请求所以浏览器是显示从缓存中该URL的内容,而不命中数据库。

Problem was causing by http response headers. Headers was contain cacheable information for ajax requests so browser was showing that url content from cache without hit to database.

从响应头删除缓存PARAMS之后再浏览器能打到服务器,而不会带来从缓存中的内容。

After removing cache params from response headers then browser was able to hit server without brings content from cache.