Aaron Moodie
Aaron Moodie

Reputation: 1965

Ajax and browser cache issue

I have small web site running on Sinatra which is updating content via ajax on a xhr request.

javascript

function get_shows() {
  $.ajax({
    type: 'GET',
    dataType: 'HTML',
    url: '/update/',
    success: function(data) {
      $('#show_list').fadeOut('fast', function(){
        $(this).html(data).fadeIn('fast');
      });
    }, 
    error:function(data){console.log(data.statusText)}
  });
}

ruby

get '/update/' do
  if request.xhr?
     erb :index_show_list, :layout => false
  else 
    erb :index  
  end
end

The issue I'm having is when a user updates content via ajax, the browser cache for that page updates and only shows the snippet fetched, and all the head and body tags are gone. The page continues to render ok, until you leave the page and then return via the back back button, in which case all that is displayed is the html snippet sans the rest of the page.

Upvotes: 0

Views: 349

Answers (1)

mscccc
mscccc

Reputation: 2275

I've had this same issue. Try setting cache to false in your ajax request. By default it is set to true.

See http://api.jquery.com/jQuery.ajax/ for more info.

Example:

function get_shows() {
  $.ajax({
    type: 'GET',
    cache: false,
    dataType: 'HTML',
    url: '/update/',
    success: function(data) {
      $('#show_list').fadeOut('fast', function(){
        $(this).html(data).fadeIn('fast');
      });
    }, 
    error:function(data){console.log(data.statusText)}
  });
}

Upvotes: 1

Related Questions