目录导读:
1\ axios 基本特征
2\ axios 基础用法
3\ axios 响应结果
4\ axios 全局配置
5\ axios 拦截器
1\ async/await 基本用法
2\ async/await 处理多个异步请求
接口调用 — axios 用法
1\ axios 基本特征
axios (官网:https://github.com/axios/axios) 是一个基于 Promise 用于浏览器和 node.js 的 HTTP 客户端。或者参考。
特征如下:
- 支持浏览器和 node.js
- 支持 promise
- 能拦截请求和响应
- 自动转换 JSON 数据
- 能转换请求和响应数据
2\ axios 基础用法
-
get 和 delete 请求传递参数
- 通过传统的 url 以 ?的形式传递参数
- restful 形式传递参数
- 通过 params 形式传递参数
-
post 和 put 请求传递参数
- 通过选项传递参数
- 通过 URLSearchParams 传递参数
代码示例:
# 1. 发送get 请求
axios.get('http://localhost:3000/adata').then(function(ret){
# 拿到 ret 是一个对象 所有的对象都存在 ret 的data 属性里面
// 注意data属性是固定的用法,用于获取后台的实际数据
console.log(ret)
})
# 2. get 请求传递参数
# 2.1 通过传统的url 以 ? 的形式传递参数
axios.get('http://localhost:3000/axios?id=123').then(function(ret){
console.log(ret.data)
})
# 2.2 restful 形式传递参数,即把参数直接放到 / 后
axios.get('http://localhost:3000/axios/123').then(function(ret){
console.log(ret.data)
})
# 2.3 通过params 形式传递参数
axios.get('http://localhost:3000/axios', {
params: {
id: 789 }
}).then(function(ret){
console.log