<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8"> <title>TestAjax</title> <style> #result{ width: 200px; height: 100px; border: solid 1px #2a5caa; } </style></head><body> <button>发送请求</button> <button>取消请求</button> <script> const btns = document.querySelectorAll('button'); let x = null; btns[0].onclick = function (){ x = new XMLHttpRequest(); x.open("GET",'http://localhost:8000/cancel') x.send(); console.log("发送请求"); } // 请求取消 btns[1].onclick = function (){ x.abort(); } </script></body></html>