使用jQuery加载缓存脚本时处理404s [英] Handling 404s while loading cached scripts using jQuery

查看:91
本文介绍了使用jQuery加载缓存脚本时处理404s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 jQuery 加载JS文件(原因超出了本文的范围)。如果可用,我希望使用缓存。此外,我无法将全局ajax 缓存设置为 true (再次出于范围之外的原因)。这排除了我使用 getScript 方法所以,按照 jQuery的网站我做了以下事情:

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:

$(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);  
});

现在这适用于所有情况,除非脚本请求返回404,没有错误或失败方法被调用。

Now this works well in all cases except when the script request returns a 404, none of the error or fail methods get called.

我可以强制它通过应用超时来调用,但我不想。
为什么不调用错误处理方法?

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?

小提琴 https://jsfiddle.net/15kozdpf/4/

推荐答案

似乎是jQuery 1中的一个错误。

Seems like a bug in jQuery 1.

如果删除 dataType:script,所有内容工作(无论你要求的URL)。

If you remove the dataType: "script", everything works (regardless the URL you request).

另一个选项 - 更新到jQuery 2:

https://jsfiddle.net/6asrte12/

Another option - upgdate to jQuery 2:
https://jsfiddle.net/6asrte12/

这篇关于使用jQuery加载缓存脚本时处理404s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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