1、原生js
var url = `http://127.0.0.1:5500/mock/index.json`/* 1、创建ajax的核心对象 */var xhr = new XMLHttpRequest();/* 2、需要与服务器建立连接 */xhr.open("get", url, true)/* 3、发送请求 */xhr.send();/* 4、响应数据 *//* onreadystatechange监听服务器状态的变化 */xhr.onreadystatechange = function () {    //   readystate值代表服务器响应的变化  ==4  请求完成,响应准备就绪    /* status==200请求成功,数据成功的响应 */    if (xhr.readyState == 4 && xhr.status == 200) {        var res = JSON.parse(xhr.responseText)        console.log(res)    }}
2、ajax.js
function $ajax(url,success){    var xhr = new XMLHttpRequest();    xhr.open("get",url,true);    xhr.send();    xhr.onreadystatechange =  function(){        if(xhr.readyState == 4  && xhr.status==200){            var res = JSON.parse(xhr.responseText);            success(res)        }    }}
<script src="lib/ajax.js"></script></head><body>    <script>        var url  = 'http://47.108.197.28:8000/book';        $ajax(url,res=>{            console.log(res)        })    </script></body>
3、obj-ajax.js
function $ajax({url,success}){    var xhr = new XMLHttpRequest();    xhr.open("get",url,true);    xhr.send();    xhr.onreadystatechange =  function(){        if(xhr.readyState == 4  && xhr.status==200){            var res = JSON.parse(xhr.responseText);            success(res)        }    }}
<script src="lib/obj-ajax.js"></script></head><body>    <script>        var url  = 'http://47.108.197.28:8000/book';        $ajax({            url,            success:res=>{                console.log(res)            }        })    </script></body>