Internet Explorer won't update AJAX handled content on refresh?

人盡茶涼 提交于 2021-02-07 19:43:40

问题


I have a page that grabs content from a XML file through a Jquery AJAX call.

The problem is it will update the content from the XML file on a refresh for every browser except IE.

I have tried to solve this with meta tags

<meta http-equiv="expires" content="-1"/>
<meta http-equiv="cache-control" content="no-cache,must-revalidate" />
<meta http-equiv="pragma" content="no-cache"/>

This is a small portion of the relevant javascript

$(document).ready(function(){ 
$.ajax({type: "GET",url: "file1.xml",dataType: "xml", success: parseXml });
}

function parseXml(xml){
document.getElementById(eventMonthName).innerHTML=firstxmlvari.getElementsByTagName('month')[0].childNodes[0].nodeValue;
}

Any suggestions would be very appreciated!


回答1:


Yes, probably you run into IE's agressive caching... Try setting the HTTP headers, but something that works for me, is appending the current time to the query string like this:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "/echo/xml/",
        data: {
            _rnd: new Date().getTime()
        },
        dataType: "xml",
        success: parseXml
    });
});

function parseXml(xml) {
    alert(xml);
}

An example on JSFIDDLE: http://jsfiddle.net/WVBDc/, check the outgoing HTTP requests.




回答2:


You can also use "cache: false" option which will work in same way that Akos Lukacs mentioned. Result is the same, but you don't have to create your own Date.

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "/echo/xml/",
        cache: false,
        dataType: "xml",
        success: parseXml
    });
});



回答3:


As jQuery's .load method doesn't offer a convenient way of turning off cache, I add a timestamp parameter to my request, which is just ignored at the controller level:

$('#userDialog').load('/Users/Edit/' + someValue + '?timestamp=' + new Date().getTime(), function () {
...
});

or:

$('#userDialog').load('/Users/Create', { timestamp: new Date().getTime() }, function () {
...
});

This is indeed only needed for IE and still is as of version 10.




回答4:


Thanks, I had a similar problem (only in IE of course) with a dropdown that didn't refresh after the request. Adding a timestamp did the trick in combination with;

$(document).trigger("ready");

in the success function, cheers!



来源:https://stackoverflow.com/questions/8619966/internet-explorer-wont-update-ajax-handled-content-on-refresh

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