简体   繁体   English

Microsoft Edge缓存Ajax请求?

[英]Microsoft Edge caching Ajax requests?

So I have an ajax system for loading content on a website, it works like this: 所以我有一个用于在网站上加载内容的ajax系统,它的工作原理如下:

When clicking a button, a Javascript function is called: 单击按钮时,将调用Javascript函数:

onclick="doSomething('abc')"

My Javascript looks like this: 我的Javascript看起来像这样:

//Ajax
var xmlhttp;
function loadXMLDoc(url,cfunc) {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}
function doSomething(var) {
    loadXMLDoc("http://www.website.com/ajax/doSomething.php?var="+var, function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("response-container").innerHTML=xmlhttp.responseText;
        }
    });
}

The PHP file called just echo's text a bit like this: PHP文件只调用echo的文本,如下所示:

echo "Response here";

In general, I use this type of system to load new content in an element with a particular id each time this Javascript function is run. 通常,每次运行此Javascript函数时,我都会使用此类型的系统在具有特定id的元素中加载新内容。

This has worked perfectly for just over 3 years now, but for some reason Microsoft Edge is caching the response from this. 这已经完美地工作了3年多,但由于某种原因,Microsoft Edge正在缓解这一点的反应。 So the content isn't being generated correctly at all. 因此内容根本没有正确生成。 So you run this once, and for x amount of time it will just keep returning that same cached response. 所以你运行一次,并且在x时间内它将继续返回相同的缓存响应。

Any ideas what the correct fix for this is? 有什么想法正确解决这个问题是什么? I've seen mention of using headers, but I'm not sure how to apply a fix in this situation. 我已经看到提到使用标题,但我不确定如何在这种情况下应用修复。

We had problems with cached ajax requests on Edge and IE 10/11 too. 我们在Edge和IE 10/11上的缓存ajax请求也有问题。 Our solution was the use of the HTTP-header Cache-Control: no-cache This works without a timestamp too. 我们的解决方案是使用HTTP-header Cache-Control: no-cache这也可以在没有时间戳的情况下工作。

Yes, there is nothing inherent about XMLHttpRequest means it shouldn't be cached. 是的,XMLHttpRequest没有固有的意味着它不应该被缓存。 Browsers treat them just like other HTTP requests. 浏览器就像其他HTTP请求一样对待它们。 You can fully control how the browser caches your content using HTTP headers. 您可以完全控制浏览器使用HTTP标头缓存内容的方式。

The best way to disable cache in IE or MS Edge is to Do it: 在IE或MS Edge中禁用缓存的最佳方法是执行此操作:

Just press f12 , then go to network tab and find the forth button in tool box with this title : Always refresh from server . 只需按f12,然后转到网络选项卡,找到工具栏中的第四个按钮,标题为:始终从服务器刷新。

It will send request each time with no cache .. 它会在没有缓存的情况下每次发送请求。 在此输入图像描述

After a lot of searching and troubleshooting, I finally found the solution to fix this. 经过大量的搜索和故障排除后,我终于找到了解决这个问题的解决方案。 Adding the header Cache-Control:no-cache did not work, it actually fouled up the troubleshooting in my case. 添加标头Cache-Control:no-cache不起作用,它实际上在我的情况下弄乱了故障排除。 I had used the header Pragma:no-cache to fix the same AJAX caching issue in IE11 but it still didn't work for Edge. 我曾使用标头Pragma:no-cache来修复IE11中相同的AJAX缓存问题,但它仍然无法用于Edge。 And the Cache-Control header fixed it (to http 1.1 specs) for Chrome and FF. 并且Cache-Control标头为Chrome和FF修复了它(对于http 1.1规范)。

According to a defect in the Microsoft database, the Pragma header is ignored if there is a Cache-Control header: 根据Microsoft数据库中的缺陷,如果存在Cache-Control标头,则忽略Pragma标头:

https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10739540/ https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10739540/

So adding logic to only use Pragma for IE and Edge while only using Cache-Control for http 1.1 compliant broswers did the trick. 所以添加逻辑只使用Pragma for IE和Edge,而只使用Cache-Control用于符合http 1.1的broswers就可以了。

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

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