Microsoft Edge caching Ajax requests?

假装没事ソ 提交于 2019-12-24 01:39:27

问题


So I have an ajax system for loading content on a website, it works like this:

When clicking a button, a Javascript function is called:

onclick="doSomething('abc')"

My Javascript looks like this:

//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:

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.

This has worked perfectly for just over 3 years now, but for some reason Microsoft Edge is caching the response from this. 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.

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.


回答1:


We had problems with cached ajax requests on Edge and IE 10/11 too. Our solution was the use of the HTTP-header Cache-Control: no-cache This works without a timestamp too.




回答2:


Yes, there is nothing inherent about XMLHttpRequest means it shouldn't be cached. Browsers treat them just like other HTTP requests. You can fully control how the browser caches your content using HTTP headers.

  • Google Developers HTTP caching
  • A Beginner's Guide to HTTP Cache Headers



回答3:


The best way to disable cache in IE or MS Edge is to Do it:

Just press f12 , then go to network tab and find the forth button in tool box with this title : Always refresh from server .

It will send request each time with no cache ..




回答4:


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. 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. And the Cache-Control header fixed it (to http 1.1 specs) for Chrome and FF.

According to a defect in the Microsoft database, the Pragma header is ignored if there is a Cache-Control header:

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.



来源:https://stackoverflow.com/questions/36435567/microsoft-edge-caching-ajax-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!