如何在axios中取消/中止ajax请求 [英] how to cancel/abort ajax request in axios

查看:732
本文介绍了如何在axios中取消/中止ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将axios用于ajax请求,将reactJS + flux用于呈现UI.在我的应用程序中,有第三方时间轴(reactJS组件).时间轴可以通过鼠标滚动来管理.在发生任何滚动事件后,App会发送ajax请求以获取实际数据.服务器上的请求处理可能比下一个滚动事件更慢的问题.在这种情况下,应用可能会有几个(通常是2-3个)已被弃用的请求,因为用户会进一步滚动.这是一个问题,因为每次接收新数据时线都会开始重绘. (因为它是reactJS +流量)因此,用户会多次看到时间轴的来回移动.解决此问题的最简单方法是像jQuery一样中止先前的ajax请求.例如:

I use axios for ajax requests and reactJS + flux for render UI. In my app there is third side timeline (reactJS component). Timeline can be managed by mouse's scroll. App sends ajax request for the actual data after any scroll event. Problem that processing of request at server can be more slow than next scroll event. In this case app can have several (2-3 usually) requests that already is deprecated because user scrolls further. it is a problem because every time at receiving of new data timeline begins redraw. (Because it's reactJS + flux) Because of this, the user sees the movement of the timeline back and forth several times. The easiest way to solve this problem, it just abort previous ajax request as in jQuery. For example:

    $(document).ready(
    var xhr;

    var fn = function(){
        if(xhr && xhr.readyState != 4){
            xhr.abort();
        }
        xhr = $.ajax({
            url: 'ajax/progress.ftl',
            success: function(data) {
                //do something
            }
        });
    };

    var interval = setInterval(fn, 500);
);

如何在axios中取消/中止请求?

How to cancel/abort requests in axios?

推荐答案

Axios目前不支持取消请求.有关详细信息,请参见此问题.

Axios does not support canceling requests at the moment. Please see this issue for details.

更新:在axios v0.15中添加了取消支持.

UPDATE: Cancellation support was added in axios v0.15.

axios取消令牌API基于撤回的可取消承诺建议.

The axios cancel token API is based on the withdrawn cancelable promises proposal.

这篇关于如何在axios中取消/中止ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆