axios的封装

        function axios(method, url) {
            return new Promise((reslove, reject) => {
                let xhr = new XMLHttpRequest()
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        if (xhr.status === 200) {
                            reslove(xhr.responseText)
                        } else {
                            reject('请求失败')
                        }

                    }
                }
                xhr.open(method, url)
                xhr.send()
            })
        }
        // axios('get', '/data/cat.txt').then((res) => {
        //     console.log(res)
        // })
        async function getData() {
            let cat = await axios('get', '/data/cat.txt')
            let dag = await axios('get', '/data/dog.txt')
            console.log(dag + cat)
        }
        getData()
        // axios.get('/data/cat.txt').then((res) => {
        //     console.log(res)
        //     return axios.get('/data/dog.txt')
        // }).then((res) => { console.log(res) })
posted @ 2022-09-15 16:36  就挺秃然的  阅读(37)  评论(0)    收藏  举报