1

I have an Ajax widget that monitors the status of a connection on a web page, and alerts the user when the server can no longer be reached. It works on Windows 7, but fails on Mac OSX 10.5.8 (both Safari and Firefox).

The crucial code is here:

    (function(e){

        e.fn.checknet=function(config){
            function checkConnection(config){
                e.ajax({
                    url:config.checkURL,
                    cache:false,
                    success:function(){
                        window.checknet.conIsActive=true
                    },
                    error:function(){
                        window.checknet.conIsActive=false
                    },
                    complete:function(){
                        if(window.checknet.conIsActive){
                            connectionExtablished()
                        }
                        else{
                            connectionLost()
                        }
                    }
                })
                setTimeout(
                    function(){checkConnection(config)},
                    config.checkInterval
                )
            }
        }
    })(jQuery);

I'm calling it every five seconds. When I shut down the server, Windows browsers do indeed notice within five seconds. However, the browsers on my Mac need about two and a half minutes.

From other questions, I gather that caching can be an issue. However, I've tried inserting parameters:"defeatcache=" + new Date().getTime() to the Ajax call and $.ajaxSetup({ cache: false }); before the Ajax call; neither works.

Does anybody have any suggestions for how I can get my Mac browsers to notice the downed connection sooner?

Community
  • 1
  • 1
BlairHippo
  • 9,054
  • 10
  • 50
  • 74

3 Answers3

1

As mentioned in the comment and here How to disable Ajax caching in Safari browser?, you need to append the paramater to the URL, I'm not sure what this bit is doing parameters:"defeatcache=" but the way I have always used it is on the url:

url = url + '&nocache=' + new Date().getTime();

To modify your example where checkURL is the plain url unedited it should be

url:config.checkURL + '&nocache=' + new Date().getTime(),

If the above still does not work you want to add no-cache headers to the URL you are trying to access - for example if the url you were accessing was called "status.php" then you could try adding no-cached headers to the status.php page itself:

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.

Obviously this code above (for PHP) would be different depending on your sever side language (taken from & more examples for other server side languages here: einternals)

Community
  • 1
  • 1
rmorse
  • 638
  • 5
  • 17
  • Thanks for the suggestion, but manually adding the parameters to the URL (though I need to do it as `'?nocache=`, since those are the only params on the line) doesn't work. I'll try adding those headers to a designated target page (right now it just hits the top-level URL), but even if it works, I'd prefer a solution that doesn't need code on a second page to work. – BlairHippo Feb 27 '13 at 22:07
  • Let me know how it goes, I've read a lot about how safari caches pages and in my own experience found this the only way round it... I know you mentioned other browsers too, is this repeatable on all other browsers (ie can you check with firefox, safari & chrome just to be sure? – rmorse Feb 28 '13 at 12:56
  • Thus far, I've only checked with Firefox and Safari; I'll give Chrome a try, too. – BlairHippo Mar 04 '13 at 14:19
  • Let me know how it goes :) If possible could you set up an example/test of this occurring (ie a jsfiddle) - happy to help but would be easier if I could recreate the problem :) – rmorse Mar 05 '13 at 14:26
  • This really didn't solve the problem, but I hate to see the bounty points go to waste, and I think this was the most helpful answer offered. So, thanks. Have a cookie. :-) – BlairHippo Mar 05 '13 at 18:42
  • Haha thanks, happy to try to carry on problem solving though? let me know what happens in Chrome and if you can set up some sort of test link :) – rmorse Mar 06 '13 at 11:16
0

when you make your .ajax() call use the option cache: false

.ajax(
     {url: 'http://...',
     cache: false });

or

before your ajax call use ajaxSetup

 $.ajaxSetup({ cache: false });
jeffbeat
  • 69
  • 1
  • 5
0

I used to disable caching in the response's header. So in the service that provides the response, look into setting various fields to disable caching from that end of the call. Sorry I don't have any ready examples!! Hope this helps.

Kieveli
  • 10,548
  • 6
  • 48
  • 77