定义一个基本的axios函数实现ajax请求

json-server

  • 用来模拟服务器API,测试接口
  • 全局安装
npm install -g json-server
  • 创建一个db.json文件
{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}
  • 启动服务器
json-server --watch db.json

定义一个基本的axios

  • 包括GET/POST/PUT/DELETE
  • 支持增删改查功能的函数
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function axios({
            url,
            method='GET',
            params={},
            data={}
        }) {
            return new Promise((resolve, reject) => {
                const request = new XMLHttpRequest()

                method = method.toUpperCase()

                let queryString = ''
                Object.keys(params).forEach(key => {
                    queryString += `${key}=${params[key]}&`
                })
                if (queryString) {
                    queryString = queryString.slice(0, queryString.length-1)
                }

                request.open(method, `${url}?${queryString}`, true)
                if (method === 'GET' || method === 'DELETE') {
                    request.send()
                } else if (method === 'POST' || method === 'PUT') {
                    request.setRequestHeader('Content-Type', 'application/json;charset=utf-8')
                    request.send(JSON.stringify(data))
                }

                request.onreadystatechange = function () {
                    if (request.readyState !== 4) return

                    const { status, statusText } = request

                    if (request.status >= 200 && request.status < 300 || request.status === 304) {
                        const response = {
                            data: JSON.parse(request.response),
                            status,
                            statusText
                        }
                        resolve(response)
                    } else {
                        reject(new Error('request error status is' + status))
                    }
                }
            })
        }
    </script>
</head>
<body>
    <button οnclick="testGet()">GET</button>
    <button οnclick="testPost()">POST</button>
    <button οnclick="testPut()">PUT</button>
    <button οnclick="testDelete()">DELETE</button>
    <script>
        function testGet() {
            axios({
                url: 'http://localhost:3000/posts',
                method: 'GET',
                // params: {
                //     id: 1,
                //     xxx: 'abc'
                // }
            }).then(
                data => {
                    console.log(data)
                },
                error => {
                    alert(error)
                }
            )
        }
        
        function testPost() {
            axios({
                url: 'http://localhost:3000/posts',
                method: 'POST',
                data: {
                    'title': 'jsontttt',
                    'author': 'tttttttttttttt'
                }
            }).then(
                data => {
                    console.log(data)
                },
                error => {
                    alert(error)
                }
            )
        }

        function testDelete() {
            axios({
                url: 'http://localhost:3000/posts/4',
                method: 'DELETE'
            }).then(
                data => {
                    console.log(data)
                },
                error => {
                    alert(error)
                }
            )
        }
        
        function testPut() {
            axios({
                url: 'http://localhost:3000/posts/4',
                method: 'PUT',
                data: {
                    'title': 'XXXXXX',
                    'author': 'RRRRRR'
                }
            }).then(
                data => {
                    console.log(data)
                },
                error => {
                    alert(error)
                }
            )
        }
    </script>
</body>
</html>
发布了18 篇原创文章 · 获赞 3 · 访问量 794

猜你喜欢

转载自blog.csdn.net/qq_16049879/article/details/104651459