Safari double-submitting ajax calls

I guess we won't know if it's a bug or a feature... Anyway Safari (as it is in 10.0.2 version) still do perform conditionnal request as explained by Dan Manastireanu. The way I found to have a request not duplicated is to set a 'If-Unmodified-Since' header with the current request time. (Alternatively you can also add a timestamp parameter in url, as mentionned again by Dan Manastireanu)

I downvoted the setRequestHeader('Connection', "close") because it is a forbidden header name and throw a Refused to set unsafe header "Connection" error.

So a basic request looks like this:

var get = function(url, callbackSucess, callbackError) {
    var request = new XMLHttpRequest();
    request.open('GET', url, true);
    request.timeout = 10000;

    var now = new Date().getTime();
    request.setRequestHeader('If-Unmodified-Since', now);

    request.onreadystatechange = function() {
        if (request.readyState !== 4) {
            return;
        }
        if (request.status === 200) {
            callbackSucess(request.status);
        } else {
            callbackError(request.status);
        }
    };
    request.onerror = function (error) {
        callbackError(error);
    };
    request.ontimeout = function () {
        request.abort();
    };
    request.send(null);
}

I believe the browser is making a conditional GET request for the second call (also see the 304 response status).

On the first call there is no cached response in the browser, so it does a normal request.
On the second request the browser does first a conditional GET request, and upon seeing that its cached response is out of date it has to repeat the GET request.

As far as I know jQuery has a builtin fix for this (it automatically appends a parameter to the request url, something like _=123456789). I don't know why this is not working here.

You could try to append a request param by hand like this: '/api/private/customers.json?v='+(new Date().getTime())
Or you could try and use jQuery.ajax with cache:false, and dataType:'jsonp'

You can open Safari's Developer Tools (Web Inspector), and check the Network tab. It will tell you more about the request and response.


Adding a Connection: Close header to the API response fixes the problem.