| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <!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>
- <script src="./js/ajax.js"></script>
- <script>
- // function ajax(method,url,callback) {
- // // ajax 异步请求
- // // 第一步 实例化一个XMLHttpRequest对象
- // let xhr = new XMLHttpRequest();
- // // 第二步 调用open方法 配置请求参数
- // // open方法参数说明
- // // 第一个参数:请求方法 GET/POST
- // // 第二个参数:请求地址
- // // 第三个参数:是否异步 true/false
- // xhr.open(method, url, true);
- // // 第三步 调用send方法 发送请求
- // xhr.send();
- // // 第四步 监听状态变化
- // xhr.onreadystatechange = function () {
- // // 状态码 0 1 2 3 4
- // // 0: 请求未初始化 1: 服务器连接已建立 2: 请求已接收 3: 请求处理中 4: 请求已完成,且响应已就绪
- // if (xhr.readyState == 4) {
- // // 状态码 200 表示请求成功
- // if (xhr.status == 200) {
- // // 响应体
- // // console.log(xhr.responseText);
- // // 解析JSON字符串 转换为对象 调用JSON.parse方法
- // let jsonObj = JSON.parse(xhr.responseText);
- // // console.log(jsonObj);
- // // 访问对象属性
- // // console.log(jsonObj[0].url);
- // // 成功之后执行传递过来的回调函数
- // callback(jsonObj);
- // }
- // }
- // }
- // }
- // setTimeout 中第一个参数为回调函数 (成功之后再回来调用的函数)
- // 一般异步处理中都会使用回调函数
- // setTimeout(function(){
- // },1000)
- // ajax("GET","https://api.thecatapi.com/v1/images/searcha",function(res){
- // console.log(res);
- // });
- ajax("GET","./data/data1.json",function(res){
- console.log(res);
- ajax("GET","xxxxx",function(res){
- console.log(res);
- ajax("GET","yyyyy",function(res){
- console.log(res);
- })
- })
- });
- </script>
- </body>
- </html>
|