简体   繁体   English

在请求中止后,Chrome拒绝向相同的网址发出Ajax请求

[英]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. 我很确定这是chrome中的错误,因为它在IE 10中不会发生,并且是最近才启动的,但是基本上是在对URL进行AJAX调用并且用户在请求期间刷新浏览器时,之后,相同的网址将失败。 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. 我可以离开它,但我想了解为什么会这样,不需要此技巧。

It is because of cacheing, and because the URL is the same as it was before, hence why it ignores it. 这是因为缓存,并且由于URL与以前相同,因此为什么忽略它。 By appending a timestamp, makes the URL different, and each request goes through. 通过附加时间戳,可以使URL不同,并且每个请求都会通过。

Another option is setting cache to false (with .ajax()) which interestingly enough, simply appends a timestamp for you. 另一个选择是将缓存设置为false(使用.ajax()),这很有趣,只需为您添加时间戳即可。

.ajax() docs .ajax()文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM