Ajax 和浏览器缓存问题

发布于 2024-12-17 16:09:58 字数 725 浏览 1 评论 0原文

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

code

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 更新内容时,该页面的浏览器缓存会更新,并且仅显示获取的代码片段,以及所有 headbody< > 标签消失了。页面会继续正常渲染,直到您离开页面,然后通过后退按钮返回,在这种情况下,显示的只是 html 片段,没有页面的其余部分。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2024-12-24 16:10:00

我也遇到过同样的问题。尝试在 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)}
  });
}

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)}
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文