防止对ajax调用进行缓存的正确方法是什么? [英] What is the correct way to prevent caching on ajax calls?

查看:90
本文介绍了防止对ajax调用进行缓存的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX调用,该调用基于简单的标准参数(例如一年中的月份)从不断变化的数据库中返回值。在IE中,此函数将返回缓存数据,这是它永远不应该的。我已经监控了服务器端,但客户端未与之联系。

I have an AJAX-call, which returns values from a ever changing database, based on simple standard parameters, like month of year. In IE, this function returns cached data, which it never should. I've monitored the server side, and it isn't contacted by the client.

现在,我的标题问题已经以不同的方式被问到了,这里已经有很多次了。前两个解决方案是:

Now, my title question has been asked in different ways, many times here already. The top two solutions are:


  • 设置 cache:false

  • 传递随机数字/字符串/时间戳以使呼叫唯一。

事实是, code> cache:false 不起作用,至少在我的应用程序中不起作用。另一方面,传递一个唯一的字符串以防止缓存,这似乎是一种快速修复方法。我不喜欢那么,防止在Ajax调用上进行缓存的正确方法是什么?

The thing is though, that cache: false doesn't work, at least not in my application. On the other hand, passing a unique string to prevent caching, seems like a quick fix hack. I don't like it. So what is the correct way of preventing caching on ajax calls?

在防止缓存方面不起作用的Ajax调用:

Ajax call that doesn't work in regards of preventing caching:

$.getJSON("myURL", {
        json : jsonDataObject,
        cache: false
    }).success(function(data) {                 
        //do something with data
});  

我也尝试调用 $。ajaxSetup({cache:false}) ; 本身,在调用发生之前,但没有任何作用...

I have also tried calling $.ajaxSetup({ cache: false }); on it's own, before the calls happen, but to no effect...

推荐答案

首先总而言之,您认为在 $。getJSON()调用中将 cache 设置为false的方式是不正确的。您要将键/值对传递到服务器,因此请求URL看起来像 site.com/resource?cache=false

First of all, the way you think you're setting cache to false in your $.getJSON() call is incorrect. You're passing a key/value pair to the server so the request URL will look like site.com/resource?cache=false.

您需要使用 $。ajax发出请求() 方法,因此您实际上可以将 cache 选项设置为 false 。但是,所有这些就是您所说的快速修复hack。它将 _ = {current_timestamp} 添加到查询字符串,以便不缓存请求。

You need to make your request using the $.ajax() method so you can actually set the cache option to false. However, all this does is what you call a "quick fix hack". It adds _={current_timestamp} to the query string so that the request will not be cached.

$.ajax({
    url: 'myurl',
    type: 'GET',
    dataType: 'json',
    data: jsonDataObject,
    cache: false, // Appends _={timestamp} to the request query string
    success: function(data) {
        // data is a json object.
    }
});

在我看来,这不是快速解决方案或破解方法,而是确保您获得来自服务器的全新响应。

In my opinion that's not a quick fix or a hack, it's the correct way to ensure you get a fresh response from the server.

如果您不想每次都这样做,则可以使用自己的包装函数:

If you'd rather not do that every time, then you can just use your own wrapper function:

$.getJSONUncached = function (url, data, successCallback) {
    return $.ajax({
        url: url,
        type: 'GET',
        dataType: 'json',
        data: data,
        cache: false, // Appends _={timestamp} to the request query string
        success: successCallback
    });
};

然后,您可以将调用替换为 $。getJSON() $。getJSONUncached()

Then you can just replace your call to $.getJSON() with $.getJSONUncached()

这篇关于防止对ajax调用进行缓存的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆