Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 - AJAX Partial View is being cached...and I can't stop it

Tags:

I'm using MVC3 - i have a javascript function that uses jQuery get() to get a PartialView from a controller.

The problem is that it's being cached and i keep getting stale content back.

I've tried [OutputCache(Duration=0)] on the action, thinking it would prevent it caching, but no joy. Could it be the client caching it too?

EDIT:

I've recently been using another way to prevent caching which may be useful to some.

$.get("/someurl?_="+$.now(),function(data) {  
     // process data
});

It's obviously not as clean, but because each request passes a _=12345678 (timestamp) it's never cached.

Hope it helps.

like image 261
sambomartin Avatar asked Feb 10 '11 20:02

sambomartin


People also ask

How do I clear partial view?

You can clear the markup from the partial view using empty() : $('#EditTestSection'). empty();

What is Cache true in Ajax call?

cache:true is the default and does not always get the content from the cache. The cache-ability of an item on the browser is determined by: The response headers returned from the origin web server. If the headers indicate that content should not be cached then it won't be.


2 Answers

GET requests could be automatically cached by the browser so you could use the .ajax() function which contrary to the .get() function allows you to disabled caching:

$.ajax({
    url: '/foo',
    type: 'GET',
    cache: 'false',
    success: function(result) {

    }
});

Another possibility is to use POST:

$.post('/foo', function(result) {

});
like image 127
Darin Dimitrov Avatar answered Sep 25 '22 17:09

Darin Dimitrov


IE is particularly bad about that. You can disable all AJAX caching with the following:

$.ajaxSetup({
    cache: false
});
like image 35
Josh Anderson Avatar answered Sep 22 '22 17:09

Josh Anderson