从缓存AJAX请求prevent Chrome浏览器 [英] Prevent Chrome from caching AJAX requests

查看:208
本文介绍了从缓存AJAX请求prevent Chrome浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装一个应用程序,它的作品奇妙的Opera和Firefox,但在谷歌浏览器其缓存的AJAX请求,并给陈旧的数据!

http://gapps.qk.com.au 是应用程序。在Chrome浏览器的时候,它甚至不发送Ajax请求,但在另一个浏览器尝试它总是AJAX请求并返回数据。

有没有什么方法(阿帕奇/ PHP / HTML / JS),从这样的行为阻止Chrome浏览器?

Ajax调用:

 函数sendAjax(动作,域idelement){

                    //创建变量
                VAR XMLHTTP,
                    TMP,
                    PARAMS =行动=+行动
                             +&放大器;域=EN + codeURIComponent(域)

                    XMLHTTP =新XMLHtt prequest();
                //检查是否AJAX请求已发送
                xmlhttp.onreadystatechange =功能(){
                    如果(xmlhttp.readyState === 4和&安培; xmlhttp.status === 200){
                        $('#'+ idelement)的.html(xmlhttp.responseText);
                    }
                };
                xmlhttp.open(GET,ajax.php?+参数,真正的);
                xmlhttp.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
                //console.log(params);
                xmlhttp.send(PARAMS);

            }
sendAjax('GAPPS','example.com','GAPPS');
 

解决方案

浏览器的缓存行为有所不同在不同的设置。你不应该依赖于用户设置或用户的浏览器。这有可能使浏览器忽略头也。

有两种方式prevent缓存。

- >更改AJAX请求POST。浏览器不缓存POST请求。

- >更好的方式和放大器;好方法:添加一个额外的参数给你的请求,无论是当前的时间戳或其他唯一的数字

  PARAMS =行动=+行动
         +&放大器;域=EN + codeURIComponent(域)
         +&放大器; preventCache =+新的日期();
 

I've setup an app and it works fantastic on Opera and Firefox, but on Google Chrome it caches the AJAX request and will give stale data!

http://gapps.qk.com.au is the app. When ran in Chrome it doesn't even send the AJAX requests, but when tried in another browser it always does the AJAX request and returns data.

Is there any method (Apache/PHP/HTML/JS) to stop Chrome from doing this behavior?

The AJAX call:

function sendAjax(action,domain,idelement) {

                    //Create the variables
                var xmlhttp,
                    tmp,
                    params = "action=" + action
                             + "&domain=" + encodeURIComponent(domain)

                    xmlhttp = new XMLHttpRequest(); 
                //Check to see if AJAX request has been sent
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                        $('#'+idelement).html(xmlhttp.responseText);
                    }
                };
                xmlhttp.open("GET", "ajax.php?"+params, true);
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                //console.log(params);
                xmlhttp.send(params);

            }
sendAjax('gapps','example.com','gapps');

解决方案

The browser cache behaves differently on different settings. You should not depend on user settings or the user's browser. It's possible to make the browser ignore headers also.

There are two ways to prevent caching.

--> Change AJAX request to POST. Browsers don't cache POST requests.

--> Better Way & good way: add an additional parameter to your request with either the current time stamp or any other unique number.

params = "action=" + action 
         + "&domain=" + encodeURIComponent(domain) 
         + "&preventCache="+new Date();

这篇关于从缓存AJAX请求prevent Chrome浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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