如何在 jQuery.get 调用中设置缓存:false [英] How to set cache: false in jQuery.get call
问题描述
jQuery.get()
是带有 get 调用的 jQuery.ajax()
的简写.但是当我在 .get()
调用的数据中设置 cache:false
时,发送到服务器的是一个名为 cache 的参数,其值为 false.虽然我的目的是将带有数据的时间戳发送到服务器以防止缓存,如果我在 jQuery.ajax 数据中使用 cache: false
会发生这种情况.如何在不将 jQuery.get 调用重写为 jQuery.ajax 调用或使用
jQuery.get()
is a shorthand for jQuery.ajax()
with a get call. But when I set cache:false
in 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: false
in jQuery.ajax data. How do I accomplish this without rewriting my jQuery.get calls to jQuery.ajax calls or using
$.ajaxSetup({
// Disable caching of AJAX responses
cache: false
});
更新:感谢大家的回答.你们都是对的.但是,我希望有一种方法可以让 get 调用知道您不想缓存,或者将该值发送到底层 .ajax() 以便它知道如何处理它.
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.
我一个.寻找除目前已确定的三种方式之外的第四种方式:
I a. looking for a fourth way other than the three ways that have been identified so far:
通过 ajaxSetup 全局执行
Doing it globally via ajaxSetup
使用 .ajax 调用代替 .get 调用
Using a .ajax call instead of a .get call
通过向 .get 调用添加一个包含时间戳的新参数来手动执行此操作.
Doing it manually by adding a new parameter holding a timestamp to your .get call.
我只是觉得这个功能应该内置到 .get 调用中.
I just thought that this capability should be built into the .get call.
推荐答案
对我来说,正确的做法应该是列出的方案.ajax
或 ajaxSetup
.如果你真的想使用 get
而不是使用 ajaxSetup
那么你可以创建你自己的参数并给它当前日期/时间的值.
To me, the correct way of doing it would be the ones listed. Either ajax
or ajaxSetup
. If you really want to use get
and not use ajaxSetup
then you could create your own parameter and give it the value of the the current date/time.
不过,我会质疑您不使用其他方法之一的动机.
I would however question your motives in not using one of the other methods.
这篇关于如何在 jQuery.get 调用中设置缓存:false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!