使用AJAX在php和html上禁用缓存 [英] Disable cache on php and html with AJAX
问题描述
我的网站使用AJAX,而我的网站在AJAX准备好文档后部分加载,并且这些部分是动态的。
示例一部分:Hello#USERNAME#这对于每个ID都是不同的,当我登录到第一个ID时显示我的用户名,但是当登录到第二个ID时,这个名称不会改变,并显示第一个ID用户名。
<?php
header(Cache-Control: no-store,no-cache,must-revalidate,max-age = 0);
头(Cache-Control:post-check = 0,pre-check = 0,false);
标题(Pragma:no-cache);
?>
但缓存不会被禁用。 >解决方案
我相信这个问题有点重复。但是,如果您希望确保缓存不会发生在ajax调用上,则可以将时间戳添加到您要访问的URL的末尾。
JavaScript中的示例AJAX的准备:
var time_stamp = new Date()。getTime();
var myurl =url_here.php?timestamp =+ time_stamp;
jQuery.get(myurl,/ *更多参数* /);
/ *其他相关代码* /
以下是Google的其他来源: p>
- http://hungred.com/how-to/tutorial-stop-caching-jquery-javascript/
- http:// viralpatel .net / blogs / ajax-cache-problem-in-ie /
- 停止jQuery .load被缓存的响应
另一件事是考虑它可能不是缓存问题。您是否正确清除任何会话/ cookies? (如果你正在使用它们)
I use AJAX for my website and my website some part load after document is ready by AJAX and those parts are dynamic.
Example one part: Hello #USERNAME# this is different for every ID, When i log in to first ID show my username but when log in to second ID this name do not change and show first ID username.
I disabled cache in all PHP pages by above code:
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
But cache is not disabled.
I believe this question is a little bit of a duplicate. However, if you want to ensure that caching does not happen on ajax calls, you can append a timestamp to the end of the URL you are hitting.
Example in your JavaScript AJAX prep:
var time_stamp = new Date().getTime();
var myurl = "url_here.php?timestamp=" + time_stamp;
jQuery.get(myurl, /* more parameters */);
/* other relevant code */
Here are some other sources from Google:
- How to prevent a jQuery Ajax request from caching in Internet Explorer?
- http://hungred.com/how-to/tutorial-stop-caching-jquery-javascript/
- http://viralpatel.net/blogs/ajax-cache-problem-in-ie/
- Stop jQuery .load response from being cached
Another thing to consider is that it might not be a caching issue. Are you properly clearing any sessions/cookies? (If you are using them)
这篇关于使用AJAX在php和html上禁用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!