how to cancel/abort ajax request in axios

前端 未结 8 679
广开言路
广开言路 2020-12-05 06:19

I use axios for ajax requests and reactJS + flux for render UI. In my app there is third side timeline (reactJS component). Timeline c

8条回答
  •  無奈伤痛
    2020-12-05 06:56

    https://github.com/axios/axios#cancellation

    const CancelToken = axios.CancelToken;
                    const source = CancelToken.source();
                    let url = 'www.url.com'
    
    
                    axios.get(url, {
                        progress: false,
                        cancelToken: source.token
                    })
                        .then(resp => {
    
                            alert('done')
    
                        })
    
                    setTimeout(() => {
                        source.cancel('Operation canceled by the user.');
                    },'1000')
    

提交回复
热议问题