美文网首页
取消请求

取消请求

作者: 苍老师的眼泪 | 来源:发表于2022-05-16 14:35 被阅读0次
    1. axios
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="axios.min.js"></script>
    </head>
    <body>
        <script>
            let cancel
            axios.get('http://localhost', {
                cancelToken: new axios.CancelToken(c => cancel = c)
            }).then(res => {
                console.log(1)
            }).catch(e => {
                console.log('请求异常')
            })
    
            document.onclick = () => {
                cancel()
                console.log('已经取消了')
            }
        </script>
    </body>
    </html>
    
    1. fetch
        <script>
            const controller = new AbortController()
    
            fetch('http://localhost', {
                signal: controller.signal
            })
    
    
            document.onclick = () => {
                controller.abort()
            }
        </script>
    

    相关文章

      网友评论

          本文标题:取消请求

          本文链接:https://www.haomeiwen.com/subject/gaqwurtx.html