Bypassing Ajax Caching When Using MooTools in Joomla

I was working on some Ajax on a project and while debugging I noticed that my responses where exactly the same. Now, I know cashing is important to improve efficiency but in my situation I was using Ajax because I wanted Live responses from my webservice. At first, I thought it was my code but soon realized that the responses where being cached. At first I figure there must be something in the Ajax object to tell it to not cache the responses, but as it turns out, it wasn’t the Ajax object doing the caching but rather the browser. Now this might be an obvious piece of knowledge for someone with Aajx experience but to me it was news.

Then I saw that responses that required an ID that hadn’t been used were getting the correct answers, that was my eureka moment. If the URL is different enough, the browser will think it is a different request and get me my results. So the solution to my problem was to rather simple, create a random number and stick it to the end of the URL used on the Ajax request.

This is the code I used to generate that unique ID

var rand = '&rand='+Math.round(new Date().getTime() / 1000);

Now I just add this to the end my my request url and caching has been bypassed

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.