Getting fresh data from database

Problem
Problem is that some browsers won't refresh data got from database. They cache query results. Example:
var url:String = "http://www.example.com/get_data.php";
var req:URLRequest = new URLRequest( url );
var loader:URLLoader = new URLLoader();
loader.load( req );
So with this you can load results from php document. But when you ask for results again, some browsers (I noticed IE) will cache results and you will get same results. Refreshing the page and loading your application again will help. But you don't want user to refresh page, don't you?
Solution
Solution is pretty simple. In order to get new result you must confuse browser. You can confuse it by giving it new url to load. So, on our original url we will give it some meaningless sufix which won't do anything other than generate new url.
url += "?randomVar=" + Math.random();
Your new url will look like this now:
http://www.example.com/get_data.php?randomVar=0.8319580480456352
This way on every refresh you get different number as sufix, making your url unique.
Finally your code should be:
var url:String = "http://www.example.com/get_data.php";
url += "?randomVar=" + Math.random();
var req:URLRequest = new URLRequest( url );
var loader:URLLoader = new URLLoader();
loader.load( req );
September 21st, 2009 - 09:05
Thank you.
You don’t know how many hours I have just spent struggling to find a way to refresh the data from a URLRequest.
What a clever solution.
Many many thanks.