接口调用--axios 和 async/await 用法

目录导读: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) 是一个基于 Pr...
摘要由CSDN通过智能技术生成

目录导读:
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
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值