Hi
I have made a page thats makes an AJAX call to the server gets the time and displays it. In the OPEN method i have set

req.open("GET","/timerProg.cgi?param1="+new date(),false);

I have set the last parameter as false as i am expecting a synchronous request so that time displayed should be consistent.

I am using new date() since the browser is caching my request and displaying the same output everytime as i dont know any other alternative.

Please suggest the alternative so that browser does not cache the results and gives proper output each time.
Alternatives for auto refresh apart from the logic i have used are highly welcome

Dani AI

Generated

— the behavior you see is browser/proxy caching, and the two quick replies here point to the usual client-side workaround. As and suggested, adding a unique query string prevents cached GETs, but that is a workaround. A clean, reliable fix is to control caching from the server and avoid blocking the UI with synchronous requests.

Send explicit HTTP cache headers from the CGI so the response is not cached. At minimum include these response headers before the body:

Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Expires: 0

Emit those lines from your CGI (or via your web server configuration) along with the normal Content-Type header. That tells browsers and intermediate proxies not to cache the timer response.

On the client side, use asynchronous requests (do not block the browser) and use modern fetch options if available. For example, the fetch API accepts a cache mode (such as "no-store") to avoid client caching. Setting a request header like Cache-Control: no-cache for XMLHttpRequest can also help, but server headers are authoritative.

Troubleshooting checklist:

  • Inspect the Network tab (disable cache while devtools is open) to see response headers and if the server returns 304.
  • Test the CGI directly with curl to confirm headers are present.
  • Remember some proxies or CDN layers may cache regardless; configure those layers too.
  • If you need validation rather than full disabling, use Last-Modified/ETag on the server and allow conditional requests.

In short: client-side cache-busting works (and is what others suggested), but proper server response headers are the robust solution and avoid relying on URL hacks or synchronous calls.

Recommended Answers

All 2 Replies

Hi
You can use random function of javascript to generate a random number and use that in query string.

Hi
Try passing a randomly gerated number as query string.

Like this
req.open("GET","/timerProg.cgi?rnd="+Math.random(),false);

Regards
Anish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.