【问题标题】:HTML5 cache manifest: unsuccessful ajax calls getting fallbackHTML5 缓存清单:不成功的 ajax 调用获得回退
【发布时间】:2012-06-08 10:31:55
【问题描述】:

我有一个 HTML5 应用程序,它使用缓存清单来提供离线功能。此应用程序在联机时进行 ajax 调用,其中一些调用可以获得 403 未授权响应。

这是我的 cache.manifest 文件的底部:

NETWORK:
*

FALLBACK:
/ /offline

如果我删除回退部分,所有收到 403 响应的 ajax 调用都会按预期工作,我可以使用 jQuery 错误处理程序检测到这一点并将用户重定向到登录表单。

但是如果存在回退部分,相同的调用会得到 200 OK 响应,回退 HTML 的内容作为正文,即使服务器回复了 403,所以我无法知道用户没有经过身份验证并且必须被发送到登录页面。

我在这里遗漏了什么吗?提前致谢

【问题讨论】:

  • 您的意思是即使用户在线并且文件确实不存在,也会执行回退?
  • 是的,您希望在线通配符标志 (*) 覆盖您在回退中输入的内容。但反过来,the spec

标签: ajax html jquery cache-manifest application-cache


【解决方案1】:

将随机数作为查询参数添加到您正在寻找的 jQuery-AJAX 页面将解决该问题;即

$.ajax({
  url: "/data.html?"+ Math.random(),
  // other properties here...
});

【讨论】:

    【解决方案2】:

    来自http://alistapart.com/article/application-cache-is-a-douchebag#latest

    这里有一个参考,因为报告状态码为0,可能会发生错误,这被解释为失败:

    $.ajax( url ).always( function(response) {
     // Exit if this request was deliberately aborted
     if (response.statusText === 'abort') { return; } // Does this smell like an error?
     if (response.responseText !== undefined) {
      if (response.responseText && response.status < 400) {
       // Not a real error, recover the content    resp
      }
      else {
       // This is a proper error, deal with it
       return;
      }
     } // do something with 'response'
    });
    

    【讨论】:

      猜你喜欢
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 2013-01-14
      • 1970-01-01
      • 2010-09-21
      • 2011-01-04
      • 2016-07-04
      相关资源
      最近更新 更多