企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] [超哥总结](https://www.kancloud.cn/chengbenchao/javascript/780049) # 一个完整的 ## 1.1 get 请求 ``` <script> /* 1.创建Ajax核心对象; 2.域服务器建立连接 3.向服务器发送请求 4.响应 */ var url = "https://www.easy-mock.com/mock/5baedb1504aec42b451a8fca/day1/test"; var xhr = new XMLHttpRequest(); xhr.open("get",url); xhr.send(); xhr.onreadystatechange = function(){ if(xhr.readyState === 4 && xhr.status ==200){ var response = xhr.responseText; console.log(response); //JSON.parse 可以将字符串转为JSON对象 var name = JSON.parse(response).data.name; console.log(name) } } </script> ``` ## 1.2 post请求 ``` <script> var url = "https://www.easy-mock.com/mock/5bad81fca1b7f6239a61664d/dataTest/postTest"; var xhr = new XMLHttpRequest(); xhr.open("post",url); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xhr.send(); xhr.onreadystatechange = function(){ if(xhr.readyState ==4 && xhr.status == 200){ console.log(xhr.responseText); } } </script> ```