Search code examples
javascriptasp.netajaxgoogle-chromeiis-express

Chrome Refuses to make Ajax Request to same url after request is aborted


I'm pretty sure this is a bug in chrome as it doesn't happen in IE 10 and it just started recently, but basically when making an AJAX call to a URL and the user refreshes the browser during the request, all requests to the same url will fail after that. Even if I refresh the browser again, the request fails. The only way I could get around it is by adding a timestamp to make every request unique but this seems like a hack. Am I missing something here?

If you have an aborted request, this will never work again:

        $.getJSON("realTimeActivity/GetRealTimeData",
            function (result) {
                // Do stuff
            }
        ).fail(function (jqXHR, textStatus, errorThrown) {
            // No error message comes back
        })

Yet this works every time:

    $.getJSON("realTimeActivity/GetRealTimeData?u=" + (new Date).getTime(),
        function (result) {
            // Do stuff
        }
    ).fail(function (jqXHR, textStatus, errorThrown) {
        // No error message comes back
    })

I could just leave it but I'd like to understand why this is and not need this hack.


Solution

  • It is because of cacheing, and because the URL is the same as it was before, hence why it ignores it. By appending a timestamp, makes the URL different, and each request goes through.

    Another option is setting cache to false (with .ajax()) which interestingly enough, simply appends a timestamp for you.

    .ajax() docs

    $.ajax({
        /* ... */
        cache: false
    });