views:

46

answers:

3

This is the statement which I am referering the below code $.ajaxSetup ({ cache: false});

I have a PHP script which produces JSON. I am consuming this json using:

      $.getJSON ("GetDetails.php?id=123",
          $("#formname").serialize(),
          function(data){
              **$.ajaxSetup ({ cache: false});**
          //I do all processing with the "data"
          });

I have seen various questions for caching in the stack overflow I know this also removes the caching

  1. $.ajaxSetup ({ cache: false});
  2. By appending Time

Are there any methods to overcome the Cache. When I use this, it is still caching. I have used the same statement in $.documnet.ready() , but no use.

A: 

It's slightly unclear what you're getting at. If you want to prevent the JSON data from being cached by the browser, you should have the PHP script specify that in its headers.

See http://articles.sitepoint.com/article/php-anthology-2-5-caching

erjiang
+3  A: 

That $.ajaxSetup() call goes before your usage of AJAX methods you want to be affected by it (you just need to run it once, not before every call), like this:

$.ajaxSetup ({ cache: false});
$.getJSON ("GetDetails.php?id=123", $("#formname").serialize(), function(data){
  //I do all processing with the "data"
});
Nick Craver
+1  A: 

Instead, you can use:

$.ajax({
    url: "GetDetails.php?id=123",
    dataType: 'json',
    data: $("#formname").serialize(),
    cache: false,
    success: function(data){
                //I do all processing with the "data"
             });
});

This will disable cache for that particular query, so you can use the normal, cache-enabled version of the .ajax call for other queries. If you want to disable cache throughout your application, you can use Nick's solution.

Chetan