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.

6 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 ;)


Leave a Reply