axios请求方式详解

axios核心
安装命令 cnpm i axios --save

axios.request()

//一个完整的get请求
axios.get("",{
//需要提交给后台的数据
	params:{
		userID:""
	},
//请求头中携带token进行后台验证
	headers:{
		token:''
	},
	before:function(){
		console.log("此方法会在请求之前调用")
	}
}).then(res=>{  //成功回调函数
	console.log(res)
}).catch(function (error){ //请求失败的回调函数
	console.log(error)
})

//一个完整的post请求,与get不同的是不用params去提交参数,直接定义就好
axios.post("",{
	userId:''
},{
	headers:{
		token:"",
	}
}).then(res=>{
	console.log(res)
}).catch(function (error){
	console.log(error)
})

//前两种是缩写写法,另一种写法
axios({
	url:"/api/login",  //请求的地址
	method:"get",      //请求方法
	data:{             //post方法直接在data里面定义就可以
		userId:""
	},
	params:{           //使用get发送请求时一定得用params提交
		userId:""
	},
	headers:{          //请求头
		token:token
	}
}).then(res=>{           //成功回调函数
	console.log(res)
}).catch(function(error){
	console.log(error)
})


axios全局拦截器用法
mounted:function(){
	axios.interceptors.request.use(function(config){
		console.log("所有发送request请求都要先执行此方法")
		return config;
	})
	axios.interceptors.response.use(function(response){
		console.log("所有发送response请求都要先执行此方法")
		return response;
	})
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值