运用axios,简化ajax多次请求

345 阅读1分钟

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        function get(url) {
            let pro = new Promise(function (resolve, reject) {
                let ajax = new XMLHttpRequest()
                ajax.open('GET', url, true)
                ajax.send()
                ajax.onreadystatechange = () => {
                    if (ajax.readyState === 4) {
                        if (ajax.status === 200) {
                            resolve(ajax.response)
                        } else {
                            console.log('请求出错,错误码:' + ajax.status);
                        }
                    }
                }
            })
            return pro
        }
        async function getList() {
            let res1 = await get('a.json')
            let res2 = await get('a.json')
            console.log(res1+res2);
        }
        getList()


        //未提出为get方法时
        //async function getData() {
        //    let res = await axios.get('http://47.92.50.43:8888/sys/jslist')        //    console.log(res.data);
        //    let res1 = await axios.get('http://47.92.50.43:8888/sys/jslist')        //    console.log(res1.data);
        //}
        //getData()
    </script>