I want to load a JS file using jQuery
(for reasons out of scope of this post). I would like the cache to be used if available. Also I cannot set the global ajax cache
setting to true
(again for reasons out of scope). This rules out my using the getScript
method So, per jQuery's website I did the following:
我想使用jQuery加载一个JS文件(出于本文范围之外的原因)。如果可用,我希望使用缓存。同样,我也不能将全局ajax缓存设置设置设置为true(出于超出范围的原因)。这就排除了我使用getScript方法的可能性,因此,根据jQuery的网站,我做了以下工作:
$(function() {
options = {
dataType: "script",
cache: true,
// scheme-less URI.
url: "//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min1.js",//<--- 404
success: function() {
console.log("yes")
},
error: function() {
console.log("error")
},
always: function() {
console.log("always")
},
statusCode: {
404: function() {
console.log("404")
}
}
};
var $d = jQuery.ajax(options);
});
Now this works well in all cases except when the script request returns a 404, none of the error or fail methods get called.
这在所有情况下都很好,除了脚本请求返回404时,没有任何错误或失败方法被调用。
I can force it to get called by applying a timeout , but I dont want to. Why are the error handling methods not getting called?
我可以通过应用超时来强制它被调用,但我不想这么做。为什么不调用错误处理方法?
Fiddle: https://jsfiddle.net/15kozdpf/4/
小提琴:https://jsfiddle.net/15kozdpf/4/
2 个解决方案
#1
2
Seems like a bug in jQuery 1.
似乎是jQuery 1中的一个bug。
If you remove the dataType: "script",
everything works (regardless the URL you request).
如果您删除了dataType:“script”,那么一切都会正常工作(不管您请求的URL是什么)。
Another option - upgdate to jQuery 2:
https://jsfiddle.net/6asrte12/
另一个选项—将jQuery 2更新为:https://jsfiddle.net/6asrte12/
#2
1
Try adding https: before the URL. It works for me
尝试在URL之前添加https:。它适合我
**Fiddle:** https://jsfiddle.net/15kozdpf/3/
#1
2
Seems like a bug in jQuery 1.
似乎是jQuery 1中的一个bug。
If you remove the dataType: "script",
everything works (regardless the URL you request).
如果您删除了dataType:“script”,那么一切都会正常工作(不管您请求的URL是什么)。
Another option - upgdate to jQuery 2:
https://jsfiddle.net/6asrte12/
另一个选项—将jQuery 2更新为:https://jsfiddle.net/6asrte12/
#2
1
Try adding https: before the URL. It works for me
尝试在URL之前添加https:。它适合我
**Fiddle:** https://jsfiddle.net/15kozdpf/3/