How to solve the problem of retrieving same value by Ajax – Browser Cache Problem

You might have faced the problem of the getting same response on the consecutive request to a PHP file from Ajax. Well, what happens when making repeated GET requests to the same URL can often lead to the response coming not from the server but from the browser cache. This problem mainly occurs while using in internet explorer.

Now let’s look at the various methods to solve this problem :

1) By Using setRequestHeader() method

Well you can use the setRequestHeader() method of the XMLHttpRequest. This function adds a custom HTTP headers to the request.

Look at the example below in which I’ve used “If-Modified-Since” header, this request header is used with get method to make it conditional i.e. if the requested resource has been modified since Sat, 1 Jan 2000 00:00:00 GMT, a copy of the resource will be returned from the server as the normal get request, it will not return from the browser’s cache.

Example Code:

request.open("GET", strURL, true);
request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); //add this line
request.send(null);

2) By passing a random element to URL in each get request

The above method of sending header request might be difficult and even sometime it’s become ineffective as well. This is the method which always use and recommend you as well.

An effective way of get rid this problem is to add a random element to the URL to which the repetitive request is sent. The browser interprets this as a request to a distinct page
and returns a server page, not the cached version.

var url = “ajaxFile.php?randval=”+Math.random();

Popularity: 7% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» How to call php from ajax in every second using Jquery
» Solving European characters (Western charset) problem with Ajax and PHP
» Solving Floating point number precision lost problem in PHP
» Solving ‘Warning: Cannot add header information’ in PHP

8 Comments on “How to solve the problem of retrieving same value by Ajax – Browser Cache Problem”

  • Stephen Cronin wrote on 15 February, 2008, 0:59

    Good tip! I haven’t tried this. I typically use:

    header(’Content-Type: text/xml’);
    header(’Cache-control: no-cache’);

    in the php file on the server side which should tell the browser that we are returning XML content & not to cache it.

    This should do the same thing, but there may be times when you don’t have control of the server side and this wouldn’t be feasible… Making a note of your tips!

  • JCR wrote on 3 April, 2008, 11:30

    Your post helped me fix a problem. Thank you. I used an incremented variable as a way to differentiate server calls.

  • Roshan wrote on 3 April, 2008, 15:13

    welcome JCR……

  • realmahbub wrote on 11 July, 2008, 10:01

    Nice and helpful information. I have got it useful.
    Thanks

  • daft01 wrote on 14 April, 2009, 8:40

    easy as pie

Trackbacks

  1. PHP Coding School » Blog Archive » php tips [2008-01-28 11:31:06]
  2. Blog » Blog Archive » How to solve the problem of retrieving same value by Ajax …
  3. How to call php from ajax in every second using Jquery

Write a Comment

 


Copyright © 2009 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.