Prevent browser caching of AJAX call result

后端 未结 21 1289
醉酒成梦
醉酒成梦 2020-11-22 04:47

It looks like if I load dynamic content using $.get(), the result is cached in browser.

Adding some random string in QueryString seems to solve this iss

21条回答
  •  抹茶落季
    2020-11-22 05:16

    I use new Date().getTime(), which will avoid collisions unless you have multiple requests happening within the same millisecond:

    $.get('/getdata?_=' + new Date().getTime(), function(data) {
        console.log(data); 
    });
    

    Edit: This answer is several years old. It still works (hence I haven't deleted it), but there are better/cleaner ways of achieving this now. My preference is for this method, but this answer is also useful if you want to disable caching for every request during the lifetime of a page.

提交回复
热议问题