DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Why does AJAX cache data?

  1. #1
    Join Date
    Mar 2007
    Posts
    2

    Why does AJAX cache data?

    Before I send request to server side, I disable the cache like this:

    function getDataFromServlet(){
    http.open('GET' ,url, true);
    http.onreadystatechange = handleHttpResponse;
    http.setRequestHeader('Cache-Control', 'no-cache');
    http.send(null);
    }

    The problem is:
    the returned data is always same. I dont know why?
    Is AJAX caching data?

    How to fix this problem ?

    thanks

  2. #2
    Join Date
    Mar 2007
    Posts
    1
    IF you use IE, it will response the data that has been cached, so it does not go to the server to request the fresh data. hence, it's not used for http.setRequestHeader('Cache-Control', 'no-cache');

    Solution, you can add the url with one parameter of sid=Math.random();
    such as 'http://www.example.com/get.php?sid=' + Math.random() on your url to make the url not the same with the previous request, so the IE will thought that it's a new url that has not been requested before

  3. #3
    Join Date
    Mar 2007
    Posts
    2
    thank you

    What If the random number generated to times. For example 4 requests could be :
    request 1: 'http://www.example.com/get.php?sid=2'
    request 2: 'http://www.example.com/get.php?sid=0'
    request 3: 'http://www.example.com/get.php?sid=9'
    request 4: 'http://www.example.com/get.php?sid=2' here data is cached

    For the time been, I have changed the browser settings to get data for every visit. In the production server, we use https. HTTPS does NOT allow the browser to cache data. My application will be Ok in Production.


  4. #4
    Join Date
    Mar 2007
    Location
    Plainfield, IL
    Posts
    1
    A better solution would be to use the datetime instead of a random number:
    'http://www.example.com/get.php?sid=' + new Date().getTime();

    This should give you very unique sequential numbers that I think are comparable to java's System.getCurrentTimeInMillis();



    Quote Originally Posted by khaled_288
    thank you

    What If the random number generated to times. For example 4 requests could be :
    request 1: 'http://www.example.com/get.php?sid=2'
    request 2: 'http://www.example.com/get.php?sid=0'
    request 3: 'http://www.example.com/get.php?sid=9'
    request 4: 'http://www.example.com/get.php?sid=2' here data is cached

    For the time been, I have changed the browser settings to get data for every visit. In the production server, we use https. HTTPS does NOT allow the browser to cache data. My application will be Ok in Production.


Similar Threads

  1. OVERSEAS Online Data Entry Information
    By jai in forum Careers
    Replies: 0
    Last Post: 02-21-2003, 12:43 AM
  2. Is inheritance overrated?
    By Phil Weber in forum .NET
    Replies: 67
    Last Post: 12-15-2002, 02:37 AM
  3. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 10:53 AM
  4. Replies: 0
    Last Post: 04-10-2000, 10:27 AM
  5. client cache data updated dinamicly
    By SBeaulac in forum VB Classic
    Replies: 0
    Last Post: 03-27-2000, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center