Axios

1.Axios简介


特性

  • 从浏览器中创建 XMLHttpRequests
  • 从 node.js 创建 http 请求
  • 支持 Promise API
  • 拦截请求和响应
  • 转换请求数据和响应数据
  • 取消请求
  • 自动转换 JSON 数据
  • 客户端支持防御 XSRF

浏览器支持

  • 谷歌浏览器
  • 火狐浏览器
  • IE7及其以上
  • Win10 Edge
  • Safari

安装

npm安装:

npm install axios

bower安装:

bower install axios

使用cdn:

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



2.Axios使用


使用Axios发送GET请求

1.直接在将请求参数拼接到请求路径上

// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345').then(function(response) {
	console.log(response);
}).catch(function(error) {
	console.log(error);
});

2.将参数放在请求列表

// 可选地,上面的请求可以这样做
axios.get('/user', {
	params: {
		ID: 12345
	}
}).then(function(response) {
	console.log(response);
}).catch(function(error) {
	console.log(error);
});

使用Axios发送POST请求

// axios发送post请求
axios.post('http://localhost:9090/post', {
	id: '123', 
	name: '小明'
}).then(function(response) {
	console.log(response);
}).catch(function(error) {
	console.log(error);
});

Axios执行并发请求

// 创建get请求
function getMu() {
	return axios.get('http://localhost:9090/get', {
		params: {
			id: 1,
			name: "小明"
		}
	});
}

// 创建post请求
function postMu() {
	let param = new URLSearchParams();
	param.append('id', '123');
	param.append('name', '小明');
	return axios.post('http://localhost:9090/post', param);
}

// 执行并发请求
axios.all([getMu(), postMu()]).then(axios.spread(function(getData, postData) {
	// 两个请求现在都执行完成
	console.log(getData);
	console.log(postData);
}));

Axios基础执行方法

待续

https://blog.csdn.net/huangpb123/article/details/78771077
https://www.jb51.net/article/109534.htm
https://blog.csdn.net/qq_39702364/article/details/79753618
https://www.2cto.com/kf/201707/653004.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值