封装一个ajax

<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <meta http-equiv="X-UA-Compatible" content="ie=edge" />

    <title>Document</title>

  </head>

  <body>

--------------------------------------------

    <script src="./node_modules/jquery/dist/jquery.js"></script>

    <script>

      function fetch(option) {

        const p = new Promise((resolve, reject) => {

          $.ajax({

            // 接口

            url: option.url,

            // 类型

            type: option.type || 'GET',

            // 数据 参数

            data: option.data || {},

            // 返回类型

            dataType: 'json',

            // 请求头

            header: option.header || {},

            // 成功回调

            success: resolve,

            // 失败回调

            error: reject

          })

        })

        return p

      }

----------------------------------------------------

      fetch({

        url:

          'http://localhost:8888/api/private/v1/login?username=admin&password=123456'

      }).then(res => {

        console.log(res)

      })

    </script>

  </body>

</html>

posted @ 2019-12-17 14:15  是闷闷啊  阅读(116)  评论(0)    收藏  举报