开发者

JQuery .load() function silently fails when offline

I'm trying to write an iPhone offline webapp using jqtouch/jquery. When I disable my connectivity, it looks like the jquery .load() function silently fails and doesn't even call its callback function. I've set $.ajaxSetup ({cache: true}); but it seems to have no effect. I also have 开发者_如何转开发my manifest file etc. working fine - the only issue is the .load() function.

Any ideas?


Are you serving your manifest files with the correct MIME type? From JQTouch Offline Support:

Also, the manifest must be served with a MIME type of text/cache-mainfest, which you can accomplish on most servers with a .htaccess directive:

AddType text/cache-manifest .manifest

To implement the Cache Manifest, simply reference it in your HTML like this:

<html manifest="sample.manifest">


Turns out they way I was calling .load() was causing it to do a POST instead of a GET, which meant it bypassed the cache.

I called it as

$.('#some-element').load('path/to/data',
                         [],
                         function(responseText, status, XMLHttpRequest) {
                           alert("Load finished: " + status + ". " + responseText);
                         }
                        );

I assumed second empty array was the correct invocation, but this made JQuery do a POST, presumably with zero arguments. The correct invocation for a GET is:

$.('#some-element').load('path/to/data',
                         function(responseText, status, XMLHttpRequest) {
                           alert("Load finished: " + status + ". " + responseText);
                         }
                        );


Local files return 0 as HTTP status code. This is because they are not retrieved with HTTP (it's local!). jQuery treats this as an error, which is not necessarily bad.

Try the onComplete handler to retrieve both status code, compare it to 0 and try to read the response text.

You may also want to test window.navigator.online (onLine?) to check if you're offline (because the status code 0 should only occur when you're offline).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜