如何在 jQuery.get 调用中设置缓存:false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8841425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:07:45  来源:igfitidea点击:

How to set cache: false in jQuery.get call

jquerycaching

提问by Barka

jQuery.get()is a shorthand for jQuery.ajax()with a get call. But when I set cache:falsein the data of the .get()call, what is sent to the server is a parameter called cache with a value of false. While my intention is to send a timestamp with the data to the server to prevent caching which is what happens if I use cache: falsein jQuery.ajax data. How do I accomplish this without rewriting my jQuery.get calls to jQuery.ajax calls or using

jQuery.get()jQuery.ajax()get 调用的简写。但是当我cache:false.get()调用的数据中设置时,发送到服务器的是一个名为缓存的参数,其值为false。虽然我的目的是将带有数据的时间戳发送到服务器以防止缓存,如果我cache: false在 jQuery.ajax 数据中使用会发生这种情况。如何在不将我的 jQuery.get 调用重写为 jQuery.ajax 调用或使用的情况下完成此操作

$.ajaxSetup({
    // Disable caching of AJAX responses
    cache: false
});

update: Thanks everyone for the answers. You are all correct. However, I was hoping that there was a way to let the get call know that you do not want to cache, or send that value to the underlying .ajax() so it would know what to do with it.

更新:谢谢大家的回答。你们都是对的。但是,我希望有一种方法可以让 get 调用知道您不想缓存,或者将该值发送到底层 .ajax() 以便它知道如何处理它。

I a. looking for a fourth way other than the three ways that have been identified so far:

我 寻找除目前已确定的三种方式之外的第四种方式:

  1. Doing it globally via ajaxSetup

  2. Using a .ajax call instead of a .get call

  3. Doing it manually by adding a new parameter holding a timestamp to your .get call.

  1. 通过 ajaxSetup 全局执行

  2. 使用 .ajax 调用而不是 .get 调用

  3. 通过向 .get 调用添加一个包含时间戳的新参数来手动执行此操作。

I just thought that this capability should be built into the .get call.

我只是认为这个功能应该内置到 .get 调用中。

采纳答案by James Montagne

To me, the correct way of doing it would be the ones listed. Either ajaxor ajaxSetup. If you really want to use getand not use ajaxSetupthen you could create your own parameter and give it the value of the the current date/time.

对我来说,正确的做法是列出的方法。无论是ajaxajaxSetup。如果您真的想使用get而不使用,ajaxSetup那么您可以创建自己的参数并为其指定当前日期/时间的值。

I would however question your motives in not using one of the other methods.

但是,我会质疑您不使用其他方法之一的动机。

回答by Kevin B

Add the parameter yourself.

自己添加参数。

$.get(url,{ "_": $.now() }, function(rdata){
  console.log(rdata);
});


As of jQuery 3.0, you can now do this:

从 jQuery 3.0 开始,您现在可以这样做:

$.get({
  url: url, 
  cache: false
}).then(function(rdata){
  console.log(rdata);
});

回答by Jivings

I think you have to use the AJAX method instead which allows you to turn caching off:

我认为您必须改用 AJAX 方法,它允许您关闭缓存:

$.ajax({
  url: "test.html",
  data: 'foo',
  success: function(){
    alert('bar');
  },
  cache: false
});

回答by Thomas Kelley

Per the JQuery documentation, .get()only takes the url, data(content), dataType, and successcallback as its parameters. What you're really looking to do here is modify the jqXHR object before it gets sent. With .ajax(), this is done with the beforeSend()method. But since .get()is a shortcut, it doesn't allow it.

根据 JQuery 文档,.get()仅将urldata(内容)dataType、 和success回调作为其参数。您真正想做的是在发送之前修改 jqXHR 对象。使用.ajax(),这是通过beforeSend()方法完成的。但由于.get()是捷径,所以不允许。

It should be relatively easy to switch your .ajax()calls to .get()calls though. After all, .get()is just a subset of .ajax(), so you can probably use all the default values for .ajax()(except, of course, for beforeSend()).

不过,.ajax().get()呼叫切换到呼叫应该相对容易。毕竟,.get()只是 的一个子集.ajax(),因此您可能可以使用所有的默认值.ajax()(当然,除了 for beforeSend())。

Edit:

编辑:

::Looks at Jivings' answer::

::看看Jivings的回答::

Oh yeah, forgot about the cacheparameter! While beforeSend()is useful for adding other headers, the built-in cacheparameter is far simpler here.

哦对了,忘记cache参数了!虽然beforeSend()对于添加其他标题很有用,但cache这里的内置参数要简单得多。

回答by Sangeet Shah

Set cache: false in jQuery.get call using Below Method

使用下面的方法在 jQuery.get 调用中设置缓存:false

use new Date().getTime(),which will avoid collisions unless you have multiple requests happening within the same millisecond.

使用 newDate().getTime(),可以避免冲突,除非在同一毫秒内发生多个请求。

Or

或者

The following will prevent all future AJAX requests from being cached, regardless of which jQuery method you use ($.get, $.ajax, etc.)

无论您使用哪种 jQuery 方法($.get、$.ajax 等),以下内容都将阻止缓存所有未来的 AJAX 请求

$.ajaxSetup({ cache: false });

回答by mar10

Note that callback syntax is deprecated:

请注意,不推荐使用回调语法:

Deprecation Notice

The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

弃用通知

jQuery 1.5 中引入的 jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调方法自 jQuery 1.8 起已弃用。要为最终删除代码做好准备,请改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。

Here a modernized solution using the promiseinterface

这是使用promise界面的现代化解决方案

$.ajax({url: "...", cache: false}).done(function( data ) {
    // data contains result
}).fail(function(err){
    // error
});

回答by RitchieD

I'm very late in the game, but this might help others. I hit this same problem with $.get and I didn't want to blindly turn off caching and I didn't like the timestamp patch. So after a little research I found that you can simply use $.post instead of $.get which does NOT use caching. Simple as that. :)

我在游戏中很晚了,但这可能会帮助其他人。我用 $.get 遇到了同样的问题,我不想盲目关闭缓存,我不喜欢时间戳补丁。因此,经过一些研究,我发现您可以简单地使用 $.post 而不是 $.get,后者不使用缓存。就那么简单。:)