【问题标题】:Jquery.get() not working in IE8/9. Won't load cached pages 304 not modifiedJquery.get() 在 IE8/9 中不起作用。不会加载缓存页面 304 未修改
【发布时间】:2012-01-17 23:35:49
【问题描述】:
  • 代码点火器版本“2.0.3”
  • jQuery 1.7
  • Jquery 历史插件

我有一个以 ajax 方式构建的 CodeIgniter 应用程序。我有一个功能如下:

$(document).on('click','.ajax_link',function(e){
    //Stop the normal href action
    e.preventDefault();

    //Grab the destination URL
    var new_url = $(this).attr('href')
    
    //Grab the content via ajax and pass it to the history change function
    $.get(base_url+new_url,function(data){
        History.pushState({
            content:data.content,
            url:data.url
        }, data.title, data.url);
        //Refresh some site variables
        refresh();
    },'json');
});

它所做的一切都是使用 ajax_link 类捕获对锚元素的点击,并将响应发送到处理该响应数据到页面中的放置的函数。

这适用于 Chrome 和 FF。我点击链接,jQuery 发出 get 请求,我返回一个 JSON 对象,我的 history.pushState() 函数将一些 json 数据注入到我的页面中。

我遇到的问题是在 IE8 中。基本上发生的事情是当我第一次打开应用程序时,链接有效,但它们只有效一次。我第二次点击链接它:

  • ajax GET
  • 收到 304(未修改)的响应
  • 不调用jQuery.get() 回调函数,因此停止死机。

如果我清除缓存,它会再次工作。所以我的假设是 IE 正在执行 get 请求,但随后它发现它过去已经请求过完全相同的文件......因此完全停止了该过程。

有人知道这个问题的解决方案吗?我在 IE 中查找了 304 错误和 ajax 和缓存错误的提及,但尚未发现与我相同的问题。

非常感谢任何帮助

(在 Windows 虚拟机 IE8 和 Internet Explorer 9 中的 IE 8 模式下测试)

已解决

只需将以下代码添加到我的 document.ready 函数中,问题就消失了。

$.ajaxSetup ({cache: false});

【问题讨论】:

    标签: jquery ajax caching internet-explorer-8 http-status-code-304


    【解决方案1】:

    改变这一行:

    var new_url = $(this).attr('href')
    

    到这里:

    var new_url = $(this).attr('href') + '?' + Math.random();
    

    这被称为“CacheBuster”,并有效地创建了一个类似于以下内容的 url:

    "website.com/page.html?1241233"
    

    每次点击“.ajax_link”的随机数都会不同,所以 IE 会认为它是一个新页面并正确获取它。

    【讨论】:

    【解决方案2】:

    我们遇到了一个问题,即 IE(和 Opera)不喜欢“application/json”,我们必须将响应呈现为“text/plain”。

    【讨论】:

    • 感谢您的建议。我设法通过在我的 document.ready 函数中放置以下内容来解决这个问题: $.ajaxSetup ({cache: false});
    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    相关资源
    最近更新 更多