使用jQuery.ajax()无法使用AJAX从iOS设备中保存到主屏幕的AJAX访问清单缓存文件 [英] Cannot access manifest-cached files with AJAX from webapp saved to Home screen in iOS devices, using jQuery.ajax()

查看:120
本文介绍了使用jQuery.ajax()无法使用AJAX从iOS设备中保存到主屏幕的AJAX访问清单缓存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个网络应用程序。它加载的一些文件包含纯静态.JSON数据。我正在使用带有dataType的jQuery.ajax()(jQuery 1.5.2):'json'和cache:true。一切正常,所有文件都已加载。我还配置了缓存清单来缓存这些JSON文件(除了css,js和images)。一切都运行良好 - 当用户访问我的网站时,我需要的所有文件都正确缓存(我有applicationCache事件处理程序,确认一切都很好)。我已经测试了谷歌浏览器中的所有内容,我可以看到所有文件(包括JSON文件)都已缓存。当我断开网络连接时,一切正常(jQuery ajax调用自动获取缓存的JSON文件)。

I'm making a webapp. Some of the files that it loads contain pure, static .JSON data. I am using jQuery.ajax() (jQuery 1.5.2) with dataType:'json' and cache:true. Everything works well and all files are loaded. I have also configured the cache manifest to cache these JSON files (in addition to css , js, and images). Everything works well there too - when a user visits my site all the files that I need are cached correctly (I have applicationCache event handlers that confirm all is well). I have tested everything in Google Chrome and I can see that all files (including the JSON ones) are cached. When I disconnect from the network, everything works (the jQuery ajax calls automagically fetch the cached JSON files).

现在我试图在移动Safari上测试这个(在实际的iPad和iPhone模拟器上)。在Safari中,一切都按预期工作 - 页面被缓存,随后在网络断开连接的访问​​中,使用缓存文件。

Now I am trying to test this on mobile Safari (on actual iPad and the iPhone simulator). Everything works as expected when in Safari - the pages are cached and on subsequent visits with network disconnected, the cached files are used.

然而,当我将应用程序添加到主屏幕在Safari中使用添加到主屏幕,应用程序启动,所有.js,.css和图像都正常工作,但我的Ajax调用没有!他们不访问缓存的.json文件。对于每个Ajax调用, XMLHttpRequest.status 0 .statusText 错误,但是 getAllResponseHeaders()显示与Safari中的功能应用程序相同(正确)的标题。

However, when I add the app to the Home Screen using "add to home screen" in Safari, the app starts up and all the .js, .css, and images work correctly, BUT my Ajax calls don't! They don't access the cached .json files. For each Ajax call the XMLHttpRequest.status is 0, .statusText is error, but getAllResponseHeaders() shows the same (correct) headers as in the functional app in Safari.

我不明白这一点。这是jQuery的.ajax()中的限制/错误还是什么?感谢任何帮助。

I don't get this. Is this a limitation/bug in jQuery's .ajax() or what? Any help is appreciated.

推荐答案

嗯,我不想回答我自己的问题,但这就是我所做的以防其他人遇到此问题:

Well, I hate to have to answer my own question, but here is what I've done in case someone else runs into this problem:

显然,当尝试在脱机模式下使用$ .ajax()时,请求成功获取缓存文件,但它返回XHMLHttpRequest.status上的0。阅读完之后,我看到当用户离线时甚至在请求本地文件时,都可以返回状态码0。但是,成功的GET应该报告200到300之间的状态代码。也许$ .ajax()检查200-300状态,否则该请求被认为是失败的。

Apparently, when trying to use $.ajax() in offline mode, the request successfully fetches the cached files, but it returns 0 on XHMLHttpRequest.status. Having read up on this, I have seen that a status code of 0 can be returned when the user is offline and even when requesting local files. However, a successful GET should report a status code between 200 and 300. Perhaps $.ajax() checks for a 200-300 status, otherwise the request is considered to have failed.

为了解决这个问题,我在我的错误处理程序中为 $ .ajax()请求添加了代码 jqXHR.responseText。** length ** 。我知道我的JSON文件中的字符数正确,所以如果 jqXHR.responseText.length 与此已知值相同,我认为请求成功并继续加载文件。我唯一需要做的就是在错误处理程序中添加 JSON.parse(jqXHR.responseText),因为数据未在状态码0上解析。

To get around this, I added code to my error handler for the $.ajax() request which checks the jqXHR.responseText.**length**. I know the correct number of characters in my JSON files, so if jqXHR.responseText.length is the same as this known value, I consider the request a success and continue loading the files. The only extra thing I have to do is add JSON.parse(jqXHR.responseText) in the error handler, because the data is not parsed on status code 0.

这篇关于使用jQuery.ajax()无法使用AJAX从iOS设备中保存到主屏幕的AJAX访问清单缓存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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