【问题标题】:Ajax and browser cache issueAjax 和浏览器缓存问题
【发布时间】:2012-01-17 23:24:28
【问题描述】:

我有一个在 Sinatra 上运行的小型网站,它通过 ajax 在 xhr 请求上更新内容。

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)}
  });
}

红宝石

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

我遇到的问题是当用户通过 ajax 更新内容时,该页面的浏览器缓存更新并且只显示获取的 sn-p,并且所有 headbody 标记都消失了。页面继续呈现正常,直到您离开页面然后通过后退按钮返回,在这种情况下,显示的只是 html sn-p 没有页面的其余部分。

【问题讨论】:

    标签: ajax sinatra


    【解决方案1】:

    我也遇到过同样的问题。尝试在您的 ajax 请求中将缓存设置为 false。默认情况下,它设置为 true。

    请参阅http://api.jquery.com/jQuery.ajax/ 了解更多信息。

    例子:

    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)}
      });
    }
    

    【讨论】:

    • 谢谢,我已经看到了,但问题是后退按钮根本不起作用。可能必须使用 History.js 来研究这个问题。有没有办法用更新的sn-p缓存常规页面?
    • 不确定我是否完全理解您在做什么。为什么不拆分主页和更新的内容?而不是让它们都由 /update/ 提供服务?我猜你的 index_show_list.erb 不包含 html/body 标签。对吗?
    猜你喜欢
    • 2015-03-02
    • 1970-01-01
    • 2015-08-05
    • 2011-10-14
    • 2011-08-18
    • 2011-04-23
    • 2012-10-08
    • 1970-01-01
    相关资源
    最近更新 更多