Vue中的接口调用

1.Fetch

  • Fetch API是新的ajax解决方案 Fetch会返回Promise
  • fetch不是ajax的进一步封装,而是原生js,没有使用XMLHttpRequest对象
<script type="text/javascript">
    /*
      Fetch API 基本用法
    */
    fetch('http://localhost:3000/fdata').then(function(data){
    
    
      // text()方法属于fetchAPI的一部分,它返回一个Promise实例对象,用于获取后台返回的数据
      return data.text();
    }).then(function(data){
    
    
      console.log(data);
    })
  </script>

fetch API 中的http请求:

  • 使用形式:fetch(url, options).then()
  • HTTP协议,它给我们提供了很多的方法,如POST,GET,DELETE,UPDATE,PATCH和PUT
  • 默认请求时get;需要在 options 对象中 指定对应的 method method:请求使用的方法 ;post 和 普通 请求的时候 需要在options 中 设置 请求头 headers 和 body;
  <script type="text/javascript">
    // GET参数传递-传统URL
    // fetch('http://localhost:3000/books?id=123', {
    
    
    //   method: 'get'
    // })
    //   .then(function(data){
    
    
    //     return data.text();
    //   }).then(function(data){
    
    
    //     console.log(data)
    //   });

    // GET参数传递-restful形式的URL
    // fetch('http://localhost:3000/books/456', {
    
    
    //   method: 'get'
    // })
    //   .then(function(data){
    
    
    //     return data.text();
    //   }).then(function(data){
    
    
    //     console.log(data)
    //   });

    // DELETE请求方式参数传递
    // fetch('http://localhost:3000/books/789', {
    
    
    //   method: 'delete'
    // })
    //   .then(function(data){
    
    
    //     return data.text();
    //   }).then(function(data){
    
    
    //     console.log(data)
    //   });

    // POST请求传参
    // fetch('http://localhost:3000/books', {
    
    
    //   method: 'post',
    //   body: 'uname=lisi&pwd=123',
    //   headers: {
    
    
    //     'Content-Type': 'application/x-www-form-urlencoded'
    //   }
    // })
    //   .then(function(data){
    
    
    //     return data.text();
    //   }).then(function(data){
    
    
    //     console.log(data)
    //   });

    // POST请求传参
    // fetch('http://localhost:3000/books', {
    
    
    //   method: 'post',
    //   body: JSON.stringify({
    
    
    //     uname: '张三',
    //     pwd: '456'
    //   }),
    //   headers: {
    
    
    //     'Content-Type': 'application/json'
    //   }
    // })
    //   .then(function(data){
    
    
    //     return data.text();
    //   }).then(function(data){
    
    
    //     console.log(data)
    //   });

    // PUT请求传参
    fetch('http://localhost:3000/books/123', {
    
    
      method: 'put',
      body: JSON.stringify({
    
    
        uname: '张三',
        pwd: '789'
      }),
      headers: {
    
    
        'Content-Type': 'application/json'
      }
    })
      .then(function(data){
    
    
        return data.text();
      }).then(function(data){
    
    
        console.log(data)
      });
  </script>

fetchAPI 中 响应格式
用fetch来获取数据,如果响应正常返回,我们首先看到的是一个response对象,其中包括返回的一堆原始字节,这些字节需要在收到后,需要我们通过调用方法将其转换为相应格式的数据,比如JSONBLOB或者TEXT等等

  <script type="text/javascript">
    fetch('http://localhost:3000/json').then(function(data){
    
    
      // return data.json();
      return data.text();
    }).then(function(data){
    
    
      // console.log(data.uname)
      // console.log(typeof data)
      var obj = JSON.parse(data);
      console.log(obj.uname,obj.age,obj.gender)
    })
  </script>

2.Axios(需要引入axios.js)

  • 基于promise用于浏览器和node.js的http客户端
  • 支持浏览器和node.js
  • 支持promise
  • 能拦截请求和响应
  • 自动转换JSON数据
  • 能转换请求和响应数据

基本使用方法:

  1. get和 delete请求传递参数 : 通过传统的url 以 ? 的形式传递参数;restful 形式传递参数;通过params 形式传递参数
  2. post 和 put 请求传递参数: 通过选项(对象形式)传递参数;通过 URLSearchParams 传递参数
  <script type="text/javascript" src="js/axios.js"></script>
  <script type="text/javascript">
    /*
      axios请求参数传递
    */
    // axios get请求传参
    // axios.get('http://localhost:3000/axios?id=123').then(function(ret){
    
    
    //   console.log(ret.data)
    // })
    // axios.get('http://localhost:3000/axios/123').then(function(ret){
    
    
    //   console.log(ret.data)
    // })
    // axios.get('http://localhost:3000/axios', {
    
    
    //   params: {
    
    
    //     id: 789
    //   }
    // }).then(function(ret){
    
    
    //   console.log(ret.data)
    // })

    // axios delete 请求传参
    // axios.delete('http://localhost:3000/axios', {
    
    
    //   params: {
    
    
    //     id: 111
    //   }
    // }).then(function(ret){
    
    
    //   console.log(ret.data)
    // })

    // axios.post('http://localhost:3000/axios', {
    
    
    //   uname: 'lisi',
    //   pwd: 123
    // }).then(function(ret){
    
    
    //   console.log(ret.data)
    // })
    // var params = new URLSearchParams();
    // params.append('uname', 'zhangsan');
    // params.append('pwd', '111');
    // axios.post('http://localhost:3000/axios', params).then(function(ret){
    
    
    //   console.log(ret.data)
    // })

    // axios put 请求传参
    axios.put('http://localhost:3000/axios/123', {
    
    
      uname: 'lisi',
      pwd: 123
    }).then(function(ret){
    
    
      console.log(ret.data)
    })
  </script>

axios 全局配置:

#  配置公共的请求头 
axios.defaults.baseURL = 'https://api.example.com';
#  配置 超时时间
axios.defaults.timeout = 2500;
#  配置公共的请求头
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
# 配置公共的 post 的 Content-Type
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

axios 拦截器:
1.请求拦截器:请求拦截器的作用是在请求发送前进行一些操作
2.响应拦截器:响应拦截器的作用是在接收到响应后进行一些操作

  <script type="text/javascript" src="js/axios.js"></script>
  <script type="text/javascript">
    axios.interceptors.request.use(function(config) {
    
    
      console.log(config.url)
      config.headers.mytoken = 'nihao';
      return config;
    }, function(err){
    
    
      console.log(err)
    })

    axios.interceptors.response.use(function(res) {
    
    
      // console.log(res)
      var data = res.data;
      return data;
    }, function(err){
    
    
      console.log(err)
    })
    axios.get('http://localhost:3000/adata').then(function(data){
    
    
      console.log(data)
    })
  </script>

猜你喜欢

转载自blog.csdn.net/weixin_42371354/article/details/104994077