0


axios 取消请求:CancelToken

注意:此方法(CancelToken),官方已经不推荐,推荐去看官网的方法

示例代码:

  1. <body>
  2. <button id="btn1">点我获取测试数据</button>
  3. <button id="btn2">取消请求</button>
  4. <script>
  5. const btn1 = document.getElementById('btn1');
  6. const btn2 = document.getElementById('btn2');
  7. const {CancelToken} = axios; //CancelToken能为一次请求‘打标识’
  8. let cancel;
  9. btn1.onclick = async () => {
  10. axios({
  11. url:'http://localhost:5000/test1?delay=3000',
  12. cancelToken: new CancelToken((c)=>{ //c是一个函数,调用c就可以关闭本次请求
  13. cancel = c;
  14. })
  15. }).then(
  16. response =>{console.log('成功了',response);},
  17. error =>{console.log('失败了',error);}
  18. )
  19. }
  20. btn2.onclick = () =>{
  21. cancel();
  22. }
  23. </script>
  24. </body>

步骤操作:

1.首先定义一个 CancelToken 来接收 axios 中 CancelToken 主要是为了’打标识

  1. const {CancelToken} = axios; //CancelToken能为一次请求‘打标识’

2.然后定义一个 cancel 变量

  1. let cancel;

3.在 axios 对象中配置 cancelToken:

  1. cancelToken: new CancelToken((c)=>{ //c是一个函数,调用c就可以关闭本次请求 cancel 的缩写
  2. cancel = c;//赋值给外面的变量cancel 提升c的作用域?
  3. })

4.最后绑定在某个事件发生后想取消请求时调用:

  1. //笔记中所写的事件为点击事件 任何事件都可以
  2. btn2.onclick = () =>{
  3. cancel();
  4. }

优化取消请求(细化错误问题):

说明:点击取消请求时,axios也会去失败的回调,但这不是服务器的错误导致的,是用户自身不需要本次请求,所以需要对于错误进行划分。以及如果用户反复点击,会发出多次请求,单只需要一次请求。

示例代码:

  1. <body>
  2. <button id="btn1">点我获取测试数据</button>
  3. <button id="btn2">取消请求</button>
  4. <script>
  5. const btn1 = document.getElementById('btn1');
  6. const btn2 = document.getElementById('btn2');
  7. const { CancelToken, isCancel } = axios //CancelToken能为一次请求‘打标识’
  8. let cancel;
  9. btn1.onclick = async () => {
  10. if(cancel) cancel();//避免多次反复请求
  11. axios({
  12. url: 'http://localhost:5000/test1?delay=3000',
  13. cancelToken: new CancelToken((c) => { //c是一个函数,调用c就可以关闭本次请求
  14. cancel = c;
  15. })
  16. }).then(
  17. response => { console.log('成功了', response); },
  18. error => {
  19. if (isCancel(error)) {
  20. //如果进入判断,证明:是用户取消了请求
  21. console.log('用户取消了请求,原因是', error.message);
  22. } else {
  23. console.log('失败了', error);
  24. }
  25. }
  26. )
  27. }
  28. btn2.onclick = () => {
  29. cancel("任性,我就是不想要了");
  30. }
  31. </script>
  32. </body>

解释:cancel 函数可在括号中添加说明,在进入失败的回调时进行判断是服务器端发出的,还是用户自己取消的。

axios 请求拦截器中使用取消请求:

示例代码:

  1. <body>
  2. <button id="btn1">点我获取测试数据</button>
  3. <button id="btn2">取消请求</button>
  4. <script>
  5. const btn1 = document.getElementById('btn1');
  6. const btn2 = document.getElementById('btn2');
  7. const { CancelToken, isCancel } = axios //CancelToken能为一次请求‘打标识’
  8. let cancel;
  9. //请求拦截器
  10. axios.interceptors.request.use(config => {
  11. if(cancel) cancel('取消了');
  12. config.cancelToken =new CancelToken((c)=> cancel = c);
  13. //c是一个函数,调用c就可以关闭本次请求
  14. return config;
  15. });
  16. //响应拦截器
  17. axios.interceptors.response.use(response => {
  18. return response.data;
  19. },error => {
  20. if (isCancel(error)) {
  21. //如果进入判断,证明:是用户取消了请求
  22. console.log('用户取消了请求,原因是', error.message);
  23. } else {
  24. console.log('失败了', error);
  25. }
  26. return new Promise(()=>{});
  27. });
  28. //进行简化
  29. btn1.onclick = async () => {
  30. const result = await axios.get('http://localhost:5000/test1?delay=3000');
  31. console.log(result);
  32. }
  33. btn2.onclick = () => {
  34. cancel("我不想要了");
  35. }
  36. </script>
  37. </body>

axios 批量发送请求:

使用了axios.all( ) 的API,括号中为数组,其中写需要并发的请求。

  1. <body>
  2. <button id="btn1">点我获取测试数据</button>
  3. <script>
  4. const btn1 = document.getElementById('btn1');
  5. btn1.onclick = async () => {
  6. axios.all([
  7. axios.get('http://localhost:5000/test1'),
  8. axios.get('http://localhost:5000/test2'),
  9. axios.get('http://localhost:5000/test3'),
  10. ]).then(
  11. response =>{console.log(response);},
  12. error =>{console.log(error);}
  13. )
  14. };
  15. </script>
  16. </body>

解释:Axios.all( ) 基于 promise.all( ),所有的都是成功的回调才会返回数据,如果有一个失败的回调,就会走错误信息。此方法会按顺序打印 并发的三个请求的数据,并且如果用了延迟请求也会是原本的顺序,这是 axios 封装好的。

标签: 前端 javascript axios

本文转载自: https://blog.csdn.net/zqlbanzhuan/article/details/128135101
版权归原作者 Allen·s 所有, 如有侵权,请联系我们删除。

“axios 取消请求:CancelToken”的评论:

还没有评论