首页 > 其他分享 >axios取消上一个请求

axios取消上一个请求

时间:2022-10-10 01:23:46浏览次数:85  
标签:axios 请求 取消 request msg https cancel font

 

 

链接,里面有效果,亲测有效果

https://codesandbox.io/s/simple-example-of-cancelling-axios-request-kyrnc?file=/src/App.vue:1074-1096

这个可能需要外网才能打开,要不然有点卡,本人亲测有效,axios版本是0.22.0.。他们版本是"axios": "0.19.2",

代码放出来

<template>
  <div id="app">
    Open browser devtools in network tab to see requests
    <hr>
    <button @click="send">Send</button>
    <button :disabled="!request" @click="cancel">Cancel</button>
    <hr>
    <DisplayRequests :requests="requests" :request="request"/>
  </div>
</template>

<script>
import axios from "axios";
import DisplayRequests from "./components/DisplayRequests";

const API_URL = "https://reqres.in/api/users?delay=2";

export default {
  name: "App",

  components: { DisplayRequests },

  data: () => ({
    requests: [],
    request: null
  }),

  methods: {
    send() {
      if (this.request) this.cancel();
      this.makeRequest();
    },

    cancel() {
      this.request.cancel();
      this.clearOldRequest("Cancelled");
    },

    makeRequest() {
      const axiosSource = axios.CancelToken.source();
      this.request = { cancel: axiosSource.cancel, msg: "Loading..." };
      axios
        .get(API_URL, { cancelToken: axiosSource.token })
        .then(() => {
          this.clearOldRequest("Success");
        })
        .catch(this.logResponseErrors);
    },

    logResponseErrors(err) {
      if (axios.isCancel(err)) {
        console.log("Request cancelled");
      }
    },

    clearOldRequest(msg) {
      this.request.msg = msg;
      this.requests.push(this.request);
      this.request = null;
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

 

 

只要关心sendd点击事件就行了

 

 

 

 

 

 

 

 

2.还可以参考

https://stackoverflow.com/questions/50516438/cancel-previous-request-using-axios-with-vue-js

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

 

标签:axios,请求,取消,request,msg,https,cancel,font
From: https://www.cnblogs.com/hechunfeng/p/16774260.html

相关文章

  • 三、Axios入门——Axios的CRUD基本使用
    一、启动json-server服务详细教程:https://www.cnblogs.com/wml-it/p/16773220.html二、搭建页面<!doctypehtml><htmllang="en"><head><metacharset="UTF-8">......
  • 案例分享-https证书链不完整导致请求失败
    背景话不多说,直接上堆栈javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIXpathbuildingfailed:sun.security.provider.certp......
  • 【Vue】axios二次封装,统一接口管理
    1.安装axiosnpminstall--saveaxios  2.创建封装js配置请求拦截器,可以在请求发送前进行统一预处理请求,不用每次在请求的时候手动添加协议头token等配置reques......
  • Springboot整合RestTemplate发送http请求
    据技术选型总结常见的三种方式发送http请求,本文介绍Springboot整合RestTemplate发送http请求方式,其他两种如下链接java原生发送http请求_程序三两行的博客HttpClient和OkHtt......
  • jmeter BeanShell PostProcessor 获取http请求的入参,存入CSV
    1、在http请求前新建一个jmeterBeanShellPostProcessor,其与http请求同在一个线程组内。  2、下载fastjson-2.0.14.jar包放在jmeter的lib目录下 下载路径为:http......
  • 微信小程序使用wx.request请求服务器json数据并渲染到页面操作示例
    wx.request({url:'http://www.likeyunba.com/test/test.json',headers:{'Content-Type':'application/json'},success:function(res){//......
  • HTTP 请求报文和响应报文
     之前有一篇文章说过,HTTP 就是用来完成 客户端和服务端通信的。 而 HTTP 报文,就是交互的内容。为了方便理解,我把客户端和服务端描述成 A 和 B 两个人。这里人......
  • HttpClient和OkHttp发送http请求
    根据技术选型总结常见的三种方式发送http请求,本问介绍框架中常用的HttpClient和OkHttp方式,其他两种如下链接​​​springboot中使用restTemplate发送http请求​​ 一、http......
  • java原生发送http请求
    根据技术选型总结常见的三种方式发送http请求,本问介绍jdk原生方式,其他两种如下链接​​httpclient和okhttp​​​​Springboot整合RestTemplate发送http请求​​使用JDK原生......
  • 如何取消默认文档的打开方式
    1.首先win+R  输入regedit弹出注册表窗口;2.左边找到HKEY_CLASSES_ROOT并展开它;3.在展开的列表中找到你要取消的文件后缀右击选择删除;4.点击是确定删除;5.然后退回到......