1. 程式人生 > 其它 >如何取消傳送ajax請求

如何取消傳送ajax請求

技術標籤:jsajax

1、abort方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<button id="yes">傳送請求</button> <button id="no">取消傳送</button> <script> let yesBtn = document.getElementById("yes") let noBtn = document.getElementById("no") let xhr; yesBtn.onclick = function
() { xhr = new XMLHttpRequest() xhr.open('get','http://127.0.0.1:8000/delay') xhr.send() } noBtn.onclick = function () { xhr.abort() } </script> </body> </html>