ajax/get请求
1 <script> 2 window.addEventListener('load', function (ev) { 3 var btn = document.getElementById('send'); 4 btn.addEventListener('click', function (ev1) { 5 // 1. 创建一个请求对象 6 var xhr = new XMLHttpRequest(); 7 8 // 2. 准备发送 9 xhr.open('get', 'http://localhost:3000/api/one', true); 10 11 // 3. 发送请求 12 xhr.send(); 13 14 // 4. 监听服务器的响应 15 xhr.addEventListener('readystatechange', function (ev2) { 16 // console.log(xhr.readyState); 17 if(xhr.readyState === 4){ 18 // 意味着服务器的响应结束了 19 // 不代表副武器的响应是一定正确 20 // console.log(xhr.status); 21 if(xhr.status === 200){ 22 console.log(xhr.response); 23 console.log(xhr.responseText); 24 } 25 } 26 }); 27 }); 28 }); 29 </script>
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步