zoukankan      html  css  js  c++  java
  • axios取消请求

    axios取消请求

    取消之前的所有pending的请求,并不是取消所有请求中 单个请求之前重复的请求

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>取消请求</title>
    </head>
    
    <body>
      <button onclick="getProducts1()">获取商品列表1</button><br>
      <button onclick="getProducts2()">获取商品列表2</button><br>
      <button onclick="cancelReq()">取消请求</button><br>
    
      <script src="https://cdn.bootcss.com/axios/0.19.0/axios.js"></script>
      <script>
        // 添加请求拦截器
        axios.interceptors.request.use((config) => {
          // 在准备发请求前, 取消未完成的请求
          if (typeof cancel === 'function') {
            cancel('取消请求')
          }
          // 添加一个cancelToken的配置
          config.cancelToken = new axios.CancelToken((c) => { // c是用于取消当前请求的函数
            // 保存取消函数, 用于之后可能需要取消当前请求
            cancel = c
          })
    
          return config
        })
    
        // 添加响应拦截器
        axios.interceptors.response.use(
          response => {
            cancel = null
            return response
          },
          error => {
            if (axios.isCancel(error)) { // 取消请求的错误
              // cancel = null
              console.log('请求取消的错误', error.message) // 做相应处理
              // 中断promise链接
              return new Promise(() => {})
            } else { // 请求出错了
              cancel = null
              // 将错误向下传递
              // throw error
              return Promise.reject(error)
            }
          }
        )
    
    
        let cancel // 用于保存取消请求的函数
        function getProducts1() {
          axios({
            url: 'http://localhost:4000/products1',
          }).then(
            response => {
              console.log('请求1成功了', response.data)
            },
            error => { // 只用处理请求失败的情况, 取消请求的错误的不用
              console.log('请求1失败了', error.message)
            }
          )
        }
    
        function getProducts2() {
    
          axios({
            url: 'http://localhost:4000/products2',
          }).then(
            response => {
              console.log('请求2成功了', response.data)
            },
            error => {
              console.log('请求2失败了', error.message)
            }
          )
        }
    
        function cancelReq() {
          // alert('取消请求')
          // 执行取消请求的函数
          if (typeof cancel === 'function') {
            cancel('强制取消请求')
          } else {
            console.log('没有可取消的请求')
          }
        }
      </script>
    </body>
    
    </html>
    

      

  • 相关阅读:
    20款时尚的 WordPress 博客主题【免费下载】
    垂涎欲滴!30个美味的食品类移动应用程序【上篇】
    Skippr – 轻量、快速的 jQuery 幻灯片插件
    Boba.js – 用于 Google 统计分析 JavaScript 库
    长期这么做的后果就是人民劳苦而得不到该有的回报,怎么能不垮
    左值与右值的根本区别在于能否获取内存地址,而能否赋值不是区分的依据
    百度后端C++电话一面
    Web 开发和数据科学家仍是 Python 开发的两大主力
    Consul架构
    去除两端逗号-JS
  • 原文地址:https://www.cnblogs.com/gengzhen/p/15242571.html
Copyright © 2011-2022 走看看