简体   繁体   中英

ajax request giving unwanted response

I have an html page index.htm in which I have a link which get the divs via ajax request to another html zoom.htm , and appends it to the body, but when I make changes in zoom.htm , the changes are not reflected, its something like the zoom.htm is saved somewhere (cached) and it is taking divs from that cached (I don't know its really cached or not) page. Even when I removed zoom.htm from my project, it is still showing its content.

$.get('zoom.htm', function(html) {
 $(html).hide().appendTo('body').fadeIn(500);
}, 'html');
  1. How can I get fresh copy of zoom.htm every time I make request.
  2. When I replace zoom.htm with zoom.jsp (this is what I want to do but I am testing with html page), its not working properly (all the html is same as that of zoom.htm).

为了防止获取缓存的响应,您只需通过添加时间戳等方法来确保每次使用的网址都是唯一的:

'zoom.htm?' + new Date().getTime()

Try to check your debugger if the response you are getting is from the cache. In Chrome Developer Tools, usually they are marked "(From Cache)". In Chrome, you can force disable the cache as well. Open developer tools and click the gear at the lower right. Then an overlay will show. Click "Disable Cache". Further requests will not use cached objects.

Also, if you are using some kind of CMS, you might want to disable the server-side cache as well if it has the option to do so


On the other hand... Are you editing the correct file? You might be editing a copy of the file and not the one you are trying to retrieve. Happens to me all the time.

The easiest way to test whether the zoom.htm beeing fetched is a cached version, is to clear your browser cache. For instance, "Edit > Preferences > Advanced: Cached Web Content => Clear Now" in Firefox.

And then, assuming your server is Apache-like, you can prevent the browser from caching your zoom.htm file using the Header directive (ie create a .htaccess file in your project directory) like this:

<filesMatch "\.(html|htm|js|css)$">
    FileETag None
    <ifModule mod_headers.c>
        Header unset ETag
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    </ifModule>
</filesMatch>

as illustrated in askapache . Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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