当前位置:   article > 正文

Axios的基本使用

Axios的基本使用

Axios

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。

特点

支持浏览器和node.js
支持promise
能拦截请求和响应
能转换请求和响应数据
能取消请求
自动转换JSON数据
浏览器端支持防止CSRF(跨站请求伪造)

安装

npm安装

$ npm install axios


bower安装

$ bower install axios


通过cdn引入

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

引入

  1.   import Axios from "axios"
  2.   Vue.prototype.$axios = Axios

使用

执行GET请求

  1. mounted(){
  2.       this.$axios.get("http://www.wwtliu.com/sxtstu/blueberrypai/getChengpinInfo.php")
  3.       .then(res => {
  4.         console.log(res.data);
  5.       })
  6.       .catch(error => {
  7.         console.log(error);
  8.       })
  9.     }

上面的请求也可以这样写:

  1. axios.get('/user', {
  2.     params: {
  3.       ID: 12345
  4.     }
  5.   })
  6.   .then(function (response) {
  7.     console.log(response);
  8.   })
  9.   .catch(function (error) {
  10.     console.log(error);
  11.   });

执行POST请求

  1. axios.post('/user', {
  2.     firstName: 'Fred',
  3.     lastName: 'Flintstone'
  4.   })
  5.   .then(function (response) {
  6.     console.log(response);
  7.   })
  8.   .catch(function (error) {
  9.     console.log(error);
  10.   });

执行多个并发请求

  1. function getUserAccount() {
  2.   return axios.get('/user/12345');
  3. }
  4. function getUserPermissions() {
  5.   return axios.get('/user/12345/permissions');
  6. }
  7. axios.all([getUserAccount(), getUserPermissions()])
  8.   .then(axios.spread(function (acct, perms) {
  9.     // 两个请求现在都执行完成
  10.   }));

响应组成

response由以下几部分信息组成

  1. {
  2.   // 服务端返回的数据
  3.   data: {},
  4.   // 服务端返回的状态码
  5.   status: 200,
  6.   // 服务端返回的状态信息
  7.   statusText: 'OK',
  8.   // 响应头
  9.   // 所有的响应头名称都是小写
  10.   headers: {},
  11.   // axios请求配置
  12.   config: {},
  13.   // 请求
  14.   request: {}
  15. }

请求方法的别名

为方便起见,为所有支持的请求方法提供了别名

  1. axios.request(config)
  2. axios.get(url[, config])
  3. axios.delete(url[, config])
  4. axios.head(url[, config])
  5. axios.post(url[, data[, config]])
  6. axios.put(url[, data[, config]])
  7. axios.patch(url[, data[, config]])

then接收以下响应信息

  1. axios.get('/user/12345')
  2.   .then(function(response) {
  3.     console.log(response.data);
  4.     console.log(response.status);
  5.     console.log(response.statusText);
  6.     console.log(response.headers);
  7.     console.log(response.config);
  8.   });

拦截器

在请求或响应被 then 或 catch 处理前拦截它们。

  1. // 添加请求拦截器
  2. axios.interceptors.request.use(function (config) {
  3.     // 在发送请求之前做些什么
  4.     return config;
  5.   }, function (error) {
  6.     // 对请求错误做些什么
  7.     return Promise.reject(error);
  8.   });
  9. // 添加响应拦截器
  10. axios.interceptors.response.use(function (response) {
  11.     // 对响应数据做点什么
  12.     return response;
  13.   }, function (error) {
  14.     // 对响应错误做点什么
  15.     return Promise.reject(error);
  16.   });

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/238626
推荐阅读
相关标签
  

闽ICP备14008679号