Day28 axios

简介: 一个 ajax 请求库。
axios发送请求过程详解
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <!-- 
        整体流程: 
        request(config) ==> dispatchRequest(config) ==> xhrAdapter(config)
        request(config): 将请求拦截器 / dispatchRequest() / 响应拦截器 通过 promise 链串连起来, 返回 promise
        dispatchRequest(config): 转换请求数据 ===> 调用 xhrAdapter()发请求 ===> 请求返回后转换响应数 据. 返回 promise
        xhrAdapter(config): 创建 XHR 对象, 根据 config 进行相应设置, 发送特定请求, 并接收响应数据, 返回 promise

     -->
    <script>
        // axios 发送请求   axios  Axios.prototype.request  bind
        //1. 声明构造函数
        function Axios(config) {
            this.config = config;
        }
        Axios.prototype.request = function (config) {
            //发送请求
            //创建一个 promise 对象
            let promise = Promise.resolve(config);
            //声明一个数组
            let chains = [dispatchRequest, undefined]; // undefined 占位
            //调用 then 方法指定回调
            let result = promise.then(chains[0], chains[1]);
            //返回 promise 的结果
            return result;
        }

        //2. dispatchRequest 函数
        function dispatchRequest(config) {
            //调用适配器发送请求
            return xhrAdapter(config).then(response => {
                //响应的结果进行转换处理
                //....
                return response;
            }, error => {
                throw error;
            });
        }

        //3. adapter 适配器
        function xhrAdapter(config) {
            console.log('xhrAdapter 函数执行');
            return new Promise((resolve, reject) => {
                //发送 AJAX 请求
                let xhr = new XMLHttpRequest();
                //初始化
                xhr.open(config.method, config.url);
                //发送
                xhr.send();
                //绑定事件
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        //判断成功的条件
                        if (xhr.status >= 200 && xhr.status < 300) {
                            //成功的状态
                            resolve({
                                //配置对象
                                config: config,
                                //响应体
                                data: xhr.response,
                                //响应头
                                headers: xhr.getAllResponseHeaders(), //字符串  parseHeaders
                                // xhr 请求对象
                                request: xhr,
                                //响应状态码
                                status: xhr.status,
                                //响应状态字符串
                                statusText: xhr.statusText
                            });
                        } else {
                            //失败的状态
                            reject(new Error('请求失败 失败的状态码为' + xhr.status));
                        }
                    }
                }
            });
        }


        //4. 创建 axios 函数
        let axios = Axios.prototype.request.bind(null);
        axios({
            method: 'GET',
            url: 'http://localhost:3000/posts'
        }).then(response => {
            console.log(response);
        });
    </script>

</head>

<body>

</body>

</html>
目录
相关文章
|
9月前
|
XML JSON 前端开发
Axios的特点
Axios的特点
44 0
|
13天前
|
JSON JavaScript 前端开发
axios浅析(一)
 axios 是一个轻量的 HTTP客户端 基于 XMLHttpRequest 服务来执行 HTTP 请求,支持丰富的配置,支持 Promise,支持浏览器端和 Node.js 端。自Vue2.0起,尤大宣布取消对 vue-resource 的官方推荐,转而推荐 axios。现在 axios 已经成为大部分 Vue 开发者的首选。
|
3月前
|
JSON 前端开发 JavaScript
axios的详细使用
axios的详细使用
84 1
|
3月前
|
XML 存储 JSON
Axios
Axios
29 0
|
3月前
|
JSON 前端开发 API
axios使用
axios使用
|
3月前
|
存储 设计模式 JSON
快速理解 Axios
快速理解 Axios
56 0
|
3月前
|
前端开发 JavaScript
Axios有哪些常用的方法?
Axios有哪些常用的方法?
49 0
|
10月前
axios详解
axios详解
67 0
|
11月前
|
JSON JavaScript 前端开发
axios的简单的使用
axios的简单的使用
47 0
|
JSON JavaScript 前端开发
axios是什么
axios是什么
149 1