user391538
user391538

Reputation:

AJAX calls not hitting server after using back button

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

Answers (3)

nnnnnn
nnnnnn

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

Burak Tamtürk
Burak Tamtürk

Reputation: 1239

This will prevent caching...

$(document).ready(function() { 
    $.get('?'+(Math.random()*20), {'format': 'json'}, function(items) { 
        console.log('hello'); 
    } 
} 

Upvotes: 0

Robin Maben
Robin Maben

Reputation: 23054

You can configure your site to not cache ajax responses.

//jQuery
$.ajaxSetup({
    cache : false
});

Upvotes: 1

Related Questions