views:

198

answers:

3

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?

A: 

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">
smountcastle
Yes. Everything else about the app works perfectly offline. My only problem is with the .load() function failing silently and not calling its callback.
edoloughlin
A: 

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);
                         }
                        );
edoloughlin
A: 

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).

Savageman
See my answer above - turns out jQuery was doing a POST.
edoloughlin