Android does not fire AJAX requests because they are cached (on jQuery mobile)

By Christian Grobmeier

Before a few days I posted about the pain I had with jQuery mobile, Phonegap and Android. One of the most confusing things was that Android did not send Ajax requests all the time I wanted it. This left my app back in nasty states. I was already assuming that the Android “browser” does some caching, but my investigations were without success.

Then I had a nice conversation on the Strut2 mailinglist on JSON security. To reason my arguments I checked out the .ajax docs from jQuery again and then… there is actually a “cache” property. And it is set to true by default.

This causes trouble with Android, but not on the iPhone. If you set this property to false, Android will send out all the Ajax requests you need.

Your request should look like:

$.ajax({
           url: "yoururl.html",
           context: document.body,
           cache : false,
           data: { 
                username :  $('#username').val(), 
                password :  $('#password').val(), 
           },
           success: function ( data ) {
               // do something
          }
    });

Try it out, when Android does not fire your AJAX requests.


Follow me on Twitter :-)

  • Dennis

    You mean “set this property to false”, I suppose. ;)

  • http://www.grobmeier.de Christian Grobmeier

    Oh indeed!! Thanks for reporting, I will update the post :-)

  • http://teamshaw.com Tim

    Thank you Christian! Exactly what I was looking for.

    Cheers.

  • Gal

    Thanks you very much for this, I’ve been racking my brain for days about this…

  • Xavier

    Thanks a lot, same as Gal…
    We solved this issue by adding a timestamp in the ajax request but your analysis saved us a lot of time…

  • http://alexgrande.com Alex Grande

    cache: false is adding a timestamp. That’s what it is doing. It is the age old trick.

    Are you sure you are sending the correct request headers, and receiving the correct response headers?

    Adding a date time to the query may be masking a bug.

  • http://www.grobmeier.de Christian Grobmeier

    @Alex: not sure what you mean. I am sending the request exactly as in the blog posts code. On server side I can see that requests do not arrive in case of cache = true (except the first one of course).

  • Pingback: More Cordova/jQuery mobile pain: ajax and events « Grobmeier on Dart, Java, Struts, PHP and more

  • Jason

    Tried this and it didnt work, any other thoughts?

  • http://www.grobmeier.de Christian Grobmeier

    For some requests it didn’t work for me either. Finally I removed the jQuey Ajax stuff and replaced it with Promise.js: https://github.com/stackp/promisejs/
    Please also read: http://www.grobmeier.de/more-cordovajquery-mobile-pain-ajax-and-events-03042012.html
    Unfortunately I am still not sure what the root cause is, I just know it’s not happening with Promise.js.

  • Mark

    I had this same issue with Android. Android (for whatever reason) like to cache PUTs and POSTs, at least for the browser that is on my phone (HTC G2) and the emulator. This was causing my ajax PUT calls to be cached on the client, and they were not actually getting to the server.

    I first tried adding cache:false to the ajax setup – however (in jQuery 1.7.1) jQuery tests the type to see if the request should have content with a regular expression (rnoContent); jQuery only applies the “_=[timestamp]” to the query string for requests with “no content”. (around lines 7482-7515 of the raw jquery-1.7.1.js) Unfortunately this means for browsers/clients (Android) that seem to like to cache PUTs and POSTs, the cache:false will not work by itself (for jQuery 1.7.1 at least).

    To solve this I just add the timestamp to the URL myself:

    $.ajax( { url: “someserver/somepath/blah” + “?_=” + jQuery.now(), type: “PUT”, data: somedata});

    I know others have suggested this solution – I just wanted to explain why it seems sometimes you apparently have to do this manually, and sometimes cache:false will work.

  • Omar

    Hi, I’m dealing with ajax post cache issue on Android 2.3.
    And it can not be solved by setting cache option to false!

    My code does almost the same thing as the original post does,
    except the page which makes the ajax call is cached by setting Cache-control to “private”.
    And it seems like when ajax post request is send from the cached page,
    the response of ajax is also cached no matter “cache : false,” is set or not.

    This problem happens when using ajax via SSL on Android 2.3 and 2.2.
    4.x with SSL is OK, and 2.x without SSL is OK, too.

  • Peter

    Thanks very much for this, it saved me hitting my head against a brick wall

  • Hansen

    This saved my day.