HTML5缓存清单:不成功的ajax调用获得后备

时间:2020-12-24 08:06:32

I've got an HTML5 application which uses a cache manifest to provide offline functionality. This application makes ajax calls, when online, and some of this calls can obtain a 403 unauthorized in response.

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

Here's the bottom of my cache.manifest file:

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

NETWORK:
*

FALLBACK:
/ /offline

If I remove the fallback section, all the ajax calls receiving a 403 response work as expected and I can detect this with jQuery error handler and redirect the user to the login form.

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

But if fallback section is present, the same calls get a 200 OK response, with fallback HTML's content as body, even though the server replied with a 403, so there's no way for me to know the user is not authenticated and must be sent to the login page.

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

Am I missing something here? Thanks in advance

我在这里错过了什么吗?提前致谢

2 个解决方案

#1


2  

Adding a random number as query-parameter to the page you're looking for to the jQuery-AJAX will solve the issue; i.e.

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

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

#2


0  

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

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

Here is a reference that errors can occur because of the reporting of the status code as 0, which is interpreted as a failure:

以下是由于状态代码报告为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'
});

#1


2  

Adding a random number as query-parameter to the page you're looking for to the jQuery-AJAX will solve the issue; i.e.

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

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

#2


0  

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

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

Here is a reference that errors can occur because of the reporting of the status code as 0, which is interpreted as a failure:

以下是由于状态代码报告为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'
});