1

Currently, I have set up an ajax call, that on change of a particular field it should execute a php script. The script first executes on page load.

AJAX Call

$j.ajax({url: prod_json_url, dataType: 'json', cache: true,
  beforeSend: function( xhr ) {
    xhr.overrideMimeType( 'text/plain; charset=x-user-defined' );
  },
  success: function(data) { 
    //things to do on success with JSON
  }
});

Everything works great in my virtual environment for testing, but when I deploy this to a different server the following happens.

1) On page load, I can view the URL in FireBug being executed and returning the correct JSON Response. Logging shows that it did go to the script to be executed and returns the correct data. Everything acts as it should.

2) I then click to update the field. By viewing Firebug in the Console again, the correct URL is being executed, however, the JSON response is incorrect. It keeps the same one that occurred on page load. When I added logging, it appears that it never actually reaches the updated URL. (Which is the same URL as page load with updated arguments).

3) If I wait some time, and try again, it sometimes behaves as it should again, but only for the one execution.

This behavior makes me believe it is a cacheing issue. Does anyone have an idea of how I can resolve this or what variable I should be looking for or checking? The database is exactly the same and I'm not sure what else might be causing this. Any help is greatly appreciated! Please let me know if more information would be helpful as well.

Rosemary
  • 167
  • 1
  • 2
  • 10
  • Try changing `cache: true` to `cache: false` and see if that clears it up. – Jeff Lambert Feb 01 '12 at 16:42
  • What server-side language are you using? You can control your headers there, and you should disable `$.ajax` cache. – yoda Feb 01 '12 at 16:42
  • Sorry, I had updated cache: false previously and it still didn't work properly on the other server. Always works in testing though. @yoda I'm using PHP. – Rosemary Feb 01 '12 at 16:46
  • You can try on server side `header("Cache-Control: no-cache, must-revalidate");` and `header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");` – yoda Feb 01 '12 at 17:19

3 Answers3

0

Set cache to false. Seems like theres client caching

El Che
  • 1,231
  • 2
  • 15
  • 33
  • There is client caching, but I'm not sure how or why that would effect making the ajax call when a field is updated. That's what I would like to fix, but I'm not sure if that is even the issue or where I should go to resolve that. – Rosemary Feb 01 '12 at 16:50
0
$(document).ready(function() {
  $.ajaxSetup({ cache: false });
});

try this copied from limk

Community
  • 1
  • 1
Jeetendra Chauhan
  • 1,917
  • 1
  • 15
  • 21
0

Try adding the current time to the url you're loading data :

prod_json_url + '?ts=' + $.now()'

Cache control HTML headers apparently are ignore on Ajax requests in IE, therefore cache:false isn't enought. This should work though.

$.now() is a shortcut to new Date().getTime();

yoda
  • 9,912
  • 19
  • 61
  • 90
  • The issue was the prod_json_url was too long, and the caching implementation was seeing it as the same url every time, even though the arguments were changing. By adding in the time earlier within the url, it resolved my issue with the caching. Thank you! – Rosemary Feb 07 '12 at 17:23