axios 用法

1. axios 特点

  1. 支持浏览器和 node.js;
  2. 支持 Promise ;
  3. 可以拦截请求和响应;
  4. 自动转换为 JSON 数据;

2. axios 响应结果属性

headers:响应头信息;
data:响应回来的实际数据;
status:响应状态码;
statusText:响应状态信息;

3. axios 常用 API

  1. get :查询数据;
  2. post :添加数据;
  3. put :修改数据;
  4. delete : 删除数据;

4. get 传递参数

get 传递参数有 url 和 params 选项两种方法;

  1. url 传递参数:参数直接写在 url 后面;
// 前端传递
axios.get('http://localhost:3000/isget?id=1').then(res => console.log(res.data); )

// 后端
app.get('/isget', (req,res) => {
	res.send('get--url 传递参数:' + req.query.id);
})
// 前端传递
axios.get('http://localhost:3000/isget/1').then(res => console.log(res.data); )

// 后端
app.get('/isget/:id', (req,res) => {
	res.send('get--Restful 传递参数:' + req.params.id);
})
  1. params 选项传递参数;
// 前端传递
axios.get('http://localhost:3000/isparams',{
	params:{
		id:1 // 这里可以传递多个参数
	}
}).then(res => console.log(res.data); )

// 后端
app.get('/isparams', (req,res) => {
	res.send('get--params 传递参数:' + req.query.id);
})

5. post 传递参数

// 前端传递
axios.post('http://localhost:3000/ispost',{
	uname:'andy',
	age:20
}).then(res => console.log(res.data); )

// 后端
axios.post('/ispost', (req,res) => {
	res.send('post 传递参数:' + res.body.uname + res.body.age);
})
// 前端传递
const params = new URLSearchParams();
params.append('uname','andy');
params.append('age','20');
axios.post('http://localhost:3000/ispost',params).then(res => console.log(res.data); )

// 后端
axios.post('/ispost', (req,res) => {
	res.send('post 传递参数:' + res.body.uname + res.body.age);
})

6. axios 全局配置

  • axios.defaults.timeout 超时时间
    设置一定的时间限制,当 axios 发送请求后,未在设定的时间按时返回数据,认为出错;
axios.default.timeout = 3000; 
  • axios.default.baseURL 请求的基准 URL 地址

在发送请求的时候,axios 会自动将基准 url 地址和 get 中的路径进行拼接;
基准 URL 方便书写,减少路径重复,帮助解决跨域问题;

// 写法一:
axios.get(http://localhost:3000/aioxs-get).then();

// 写法二:
axios.default.baseURL = 'http://localhost:3000/'
axios.get(aioxs-get).then();
  • axios.default.headers[ ' ' ] 设置请求头

请求头需要后台配置成功,前端方可向后端发送请求头;
请求头用于登录

axios.default。headers['mytoken'] = 'asdfghjtyuio7gh';

7. axios 拦截器

  1. 请求拦截器
    axios 在向服务器发送请求之前,会经过请求拦截器,根据这一特性,可以在请求拦截器中设置一些信息
    axios.intercetors.request.use( )
axios.interceptors.request.use(function(config) {
	// 在请求发出之前进行一些信息设置
	config.headers.mytoken = 'nihao'; // 设置请求头,在请求拦截器中设置请求头更加灵活
	return config;  // 信息设置完成,需要将 config 返回出去
},function(err) {
	// 处理响应的错误信息
	console.log(err);
})
  1. 响应拦截器
    在 axios 获取返回数据之前进行拦截,在响应拦截器中同样可以对数据进行一些加工处理;
    axios.intercetors.response.use( )
axios.interceptors.response.use(function(res) {
	// 在此处对返回的数据进行处理
	res = res.data;
	return res;
},funtion(err) {
	// 处理响应的错误信息
	console.log(err);
})

8. async await 基本用法

async 关键字用在函数前,使用 async 关键字的函数返回值是 Promise 实例对象;
await 关键字用在 async 函数内,异步函数之前,可以得到异步的结果;

async function getData() {
	const ret = axios.get('data'); // ret 的值为 axios 异步请求得到的数据
	return ret;
}

getData.then(ret => {
	console.log(ret);
})
在Vue中使用axios进行数据请求的用法如下: 1. 首先,在项目的main.js文件中导入axios并将其挂载到Vue的原型上,以便在整个项目中都可以使用axios进行请求。例如:import axios from 'axios' Vue.prototype.$axios = axios。 2. 在需要发送请求的组件中,可以通过this.$axios来调用axios的各种方法进行数据请求。 - 使用get方法发送get请求,可以通过传入请求的url和可选的参数进行请求。例如:this.$axios.get(url, params)。 - 使用post方法发送post请求,可以通过传入请求的url和需要发送的数据进行请求。例如:this.$axios.post(url, data)。 - 使用put方法发送put请求,可以通过传入请求的url和需要修改的数据进行请求。例如:this.$axios.put(url, data)。 - 使用delete方法发送delete请求,可以通过传入请求的url和可选的参数进行请求。例如:this.$axios.delete(url, params)。 3. 可以通过axios的拦截器来对请求或响应进行处理。 - 使用axios的请求拦截器,可以在请求发送前对请求的配置进行修改。例如:axios.interceptors.request.use(config => return config)。 - 使用axios的响应拦截器,可以对服务器返回的数据进行处理并返回处理后的数据。例如:axios.interceptors.response.use(res => return res.data)。 4. 创建axios实例可以通过axios.create({})来创建,可以传入一些参数进行配置,如baseUrl、method和timeout等。例如:axios.create({})。 5. 可以通过axios.defaults来设置全局的默认配置,例如设置baseUrl、method和timeout等。例如:axios.defaults.xxx。 6. 使用axios取消请求,可以通过创建取消请求的错误对象和取消请求的token对象来实现。例如:axios.CancelToken()和axios.isCancel()。 7. 使用axios.all可以批量执行多个异步任务,传入多个promise对象进行执行。例如:axios.all(promise)。 综上所述,以上是Vue中使用axios进行数据请求的一些常用用法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue-axios使用](https://blog.csdn.net/m0_57391092/article/details/126405908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue axios用法教程详解](https://download.csdn.net/download/weixin_38513669/12776235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值