jQuery $.ajax() 的区别;和 $.ajaxSetup();
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7750447/
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
Difference between $.ajax(); and $.ajaxSetup();
提问by Sagar Ranpise
What is the difference between $.ajax();
and $.ajaxSetup();
in jQuery as in:
jQuery 中$.ajax();
和之间的区别$.ajaxSetup();
是:
$.ajax({
cache:false
});
and
和
$.ajaxSetup({
cache:true
});
Also, which one is best option?
另外,哪个是最好的选择?
回答by Wazy
The following will prevent allfuture AJAX requests from being cached, regardless of which jQuery method you use ($.get, $.ajax, etc.)
无论您使用哪种 jQuery 方法($.get、$.ajax 等),以下内容都将阻止缓存所有未来的 AJAX 请求
$(document).ready(function() {
$.ajaxSetup({ cache: false });
});
you should use $.ajax, which will allow you to turn caching off for thatinstance:
您应该使用 $.ajax,这将允许您关闭该实例的缓存:
$.ajax({url: "myurl", success: myCallback, cache: false});
回答by Ergec
ajaxSetup
sets default values to be valid for all ajax requests. After this you don't have to do the same setting in $.ajax
ajaxSetup
将默认值设置为对所有 ajax 请求都有效。在此之后,您不必在$.ajax
All settings in $.ajax
will be valid only for that ajax call.
中的所有设置$.ajax
仅对该 ajax 调用有效。
回答by alex
The first one disables cache on a per request basis, the second one sets it up to be disabled globally by default for all AJAX functions.
第一个在每个请求的基础上禁用缓存,第二个将其设置为默认情况下为所有 AJAX 功能全局禁用。
回答by Razan Paul
To avoid caching, one option is to give different URL for the same resource or data. To generate different URL, you can add a random query string to the end of the URL. This technique works for JQuery, Angular or other type ajax requests.
为了避免缓存,一种选择是为相同的资源或数据提供不同的 URL。要生成不同的 URL,您可以在 URL 末尾添加一个随机查询字符串。这种技术适用于 JQuery、Angular 或其他类型的 ajax 请求。
myURL = myURL +"?random="+new Date().getTime();
JQuery uses the similar technique via $.ajax({cache:false});
and $.ajaxSetup({cache:false});
JQuery 通过$.ajax({cache:false});
和使用类似的技术 $.ajaxSetup({cache:false});
$.ajax({cache:false})
applies the technique on which it is included, $.ajaxSetup({cache:false});
applies the technique for all AJAX functions.
$.ajax({cache:false})
应用包含它$.ajaxSetup({cache:false});
的技术, 将技术应用于所有 AJAX 函数。
回答by today
Also, which one is best option?
另外,哪个是最好的选择?
According to jQuery api documentation, using $.ajaxSetup()
is not recommended:
根据jQuery api 文档,$.ajaxSetup()
不推荐使用:
Note: The settings specified here will affect all calls to
$.ajax
or Ajax-based derivatives such as$.get()
. This can cause undesirable behavior since other callers (for example, plugins) may be expecting the normal default settings. For that reason we strongly recommend against using this API. Instead, set the options explicitly in the call or define a simple plugin to do so.
注意:此处指定的设置将影响所有调用
$.ajax
或基于 Ajax 的衍生工具,例如$.get()
. 这可能会导致不良行为,因为其他调用者(例如,插件)可能需要正常的默认设置。出于这个原因,我们强烈建议不要使用这个 API。相反,请在调用中明确设置选项或定义一个简单的插件来执行此操作。