Vue 中使用axios进行数据请求

刚开始看Vue,目前还停止在会使用API,做个笔记。

在使用axios进行网络请求时需要安装axios库,

  • 使用npm 安装 命令行 npm install axios --save
  • 使用cdn方式 
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

如果使用的是npm方式,需要在<script></script>中进行引用

<!--导出模块-->
<script>
var  axios = require('axios');
</script>
  • Get请求

1.普通的get请求 通过&进行参数的拼接

//普通的网络请求
axios.get('http://127.0.0.1:8090/home/getlist?name=张&age=30').then(function(reponse){
        console.log(reponse.data);

}).catch(function(error){
	 				//异常处理

}).finally(function(){

});

2.Get请求 通过params进行参数传递

axios.get('http://127.0.0.1:8090/home/getlist',{
	params:{
		name:'张辉',
		age:'30'
	}
}).then(function(reponse){

	console.log('param请求的参数'+reponse.data);

}).catch(function(error){

}).finally(function(){

});
  • Post请求
axios.post('http://127.0.0.1:9091/user/register/test',{
	name:'张辉',
	age:'30'
}).then(function(reponse){

	var resultJson = reponse.data;
	console.log(JSON.stringify(resultJson)+'请求结果的响应');

}).catch(function(error){

}).finally(function(){

});

学习博客:https://www.npmjs.com/package/axios

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值