Reputation:
I'm, making a simple AJAX call on page load...
$(document).ready(function() {
$.get('', {'format': 'json'}, function(items) {
console.log('hello');
}
}
If I hit a link on the page, then use the back button, the code above runs (it outputs 'hello' to the console), but the request never hits the server, it just uses the response it had originally as if it was cached.
Is it possible to force it to hit the server?
Upvotes: 3
Views: 901
Reputation: 150030
If you use the $.ajax()
method then you can set cache:false
. The following is the equivalent of your $.get()
call:
$.ajax({
url: 'yourUrlHere',
data: {'format': 'json'},
cache : false,
success: function(items) {
console.log('hello');
},
dataType: 'json'
});
Or set a default for all ajax calls:
$.ajaxSetup({cache : false});
Upvotes: 3
Reputation: 1239
This will prevent caching...
$(document).ready(function() {
$.get('?'+(Math.random()*20), {'format': 'json'}, function(items) {
console.log('hello');
}
}
Upvotes: 0
Reputation: 23054
You can configure your site to not cache ajax responses.
//jQuery
$.ajaxSetup({
cache : false
});
Upvotes: 1