Ajax cache problem for requesting same URL

May 7, 2008

Today my colleague, Al-Amin Rubel, faces a interesting (but very common of course) problem –

“Pages are not being reloaded/ refreshed after contents are updated by AJAX” that means AJAX request always returns/responses same data from server although data has already been changed. But, more interesting matter is in mozilla FIREFOX is working fine whereas I.E (internet explorer) showing same output.

After debugging a lot, it was clear that whenever request URL of ajax is exactly same as previous request, it loads the output from cache.

Solution:

So, perhaps the best solutions is to change the url every time. Don’t afraid, this is very simple, just add a url variable with random value using random() function.

Example:

Suppose, your AJAX request URL is – “ajaxresponse.php?name=xyz&res=123”

Then change it by adding a variable with random value – like

‘ajaxresponse.php?name=xyz&res=123&rnd=’+Math.random(10);

That’s it, solved!

This is because browser takes the url as a new request (url) every time.

Advertisements

8 Responses to “Ajax cache problem for requesting same URL”

  1. Rita Says:

    I don’t know how to thank you? It really resolved my caching issue that I was struggling for a week.

    Thanks a log again.

  2. rshad Says:

    You are welcome and thank you too for your reply.
    I am very much happy to know that somebody really get help from my post.

  3. alucard Says:

    thanks a lot for the fix…it really helped a lot…

  4. jjmontgo Says:

    In your code, you should have the option of setting the request method. Make sure this request method is POST and not GET. GET is meant to be cached.

  5. rshad Says:

    Hi jjmontgo,
    Thanks for your comments.
    Yes, GET is meant to be cached, but cached stuff will only be called or used when the same URL will be used/called. Here “Math.random(10)” part is differentiating the URL from being same.

  6. Anne Says:

    Thanks a lot 😉

  7. Meena Says:

    You solved a great mystery I have been living with for the past 2 days! Thanks a ton!


  8. […] post is copied from my first blog which was posted on May 7, […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: