4

I'm using jquery ajax in my mobile app to fetch my json data via restful apis. My restful apis have been configured to support etags. My restful apis are hosted on another subdomain, so I have configured CORS to handle the cross domain calls. In my dev environment, I have configured my mobile app to run on test.domain.com and the restful apis to run in test2.domain.com so that I can test the cross domain calls. All of this works great! My ajax calls behave exactly as I would expect with the etags:

Make a call to get json content:

$.ajax({
    ifmodified: true,
    ....
    success: function(data){

    }

I get a status 200 and the data variable in the success callback has the data. Make another call to the same resource (not expecting any changes) I get a status 304 and the data variable in the success callback has the data (loaded from cache).

So far so good.

Now if I configure my dev environment to have both my mobile app and restful apis on the same domain, here is where I run in to problems.

$.ajax({
    ifmodified: true,
    ....
    success: function(data){

    }

I get a status 200 and the data variable in the success callback has the data. Make another call to the same resource (not expecting any changes) I get a status 304 and the data variable in undefined!!!!

Any thoughts why with this configuration jquery won't populate the data variable with what is in cache? I also removed CORS and the crossdomain setting in my ajax call, but still no luck. I'm using jquery 1.7.1.

bmurmistro
  • 1,070
  • 1
  • 16
  • 35
  • 1
    Check if you get anything in the `complete` callback in the xhttpr argument(the first arg I believe), specifically look for `xhttpr.responseText` does it have anything? – dennmat Mar 29 '12 at 14:01
  • responseText is coming back as "" – bmurmistro Mar 29 '12 at 14:22
  • I'm not sure how much you're relying on cacheing here, but if whatever the request is for isn't too intensive you could always add the `cache: false` option (just sticks junk on the end of the request url). Also I'm not sure about the `ifmodified` option doing what you think it does: http://api.jquery.com/jQuery.ajax/ perhaps just removing that or setting it to false will fix the issue. Cheers. – dennmat Mar 30 '12 at 14:39
  • What mobile rendering engine are you testing on? I'm assuming webkit.. – Bob Gregor Apr 07 '12 at 17:48
  • Webkit, gecko, and trident. Same issue with all of them. – bmurmistro Apr 09 '12 at 13:03
  • see http://stackoverflow.com/questions/5118017/how-to-fix-browser-cache-and-notmodified-respond-for-json-jquery-ajaxifmodifi/12653101#12653101 – MarkB Oct 01 '12 at 13:56

1 Answers1

0

By default, requests are always issued, but the browser may serve results out of its cache. To disallow use of the cached results, set cache to false. To cause the request to report failure if the asset has not been modified since the last request, set ifModified to true.

Igor Escobar
  • 1,047
  • 1
  • 12
  • 13
  • What if I always want to process the 'content'? Is there a way to get the 'cached' data? – Terry Feb 17 '21 at 23:29