How to solve the problem of retrieving same value by Ajax - Browser Cache Problem
Posted on January 28, 2008
Filed Under ajax, tips and technique
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: 10% [?]
Follow me on twitter at http://twitter.com/roshanbh.
Related Posts
» How to call php from ajax in every second using Jquery
» Solving ‘Warning: Cannot add header information’ in PHP
» Uploading large(big) files in PHP using .htaccess
» How to return value from Ajax Function - Use synchronous request
Comments
7 Responses to “How to solve the problem of retrieving same value by Ajax - Browser Cache Problem”
Leave a Reply






[...] How to solve the problem of retrieving same value by Ajax … By Roshan 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 … Roshan Bhattarai’s Blog - PHP… - http://roshanbh.com.np [...]
[...] Roshan wrote an interesting post today on How to solve the problem of retrieving same value by Ajax …Here’s a quick excerptYou 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 … [...]
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!
[...] be wondering why I’ve passed “randval” to “ajaxTime.php”, you can read this post of mine about the problem of getting same value from ajax. And when the button with id “stop” is called the clearInterval() functions clears the [...]
Your post helped me fix a problem. Thank you. I used an incremented variable as a way to differentiate server calls.
welcome JCR……
Nice and helpful information. I have got it useful.
Thanks