傻瓜式入门,简单粗暴说用法
通过cdn引入js文件
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
get请求,默认’Content-Type’: ‘application/json’,可在头部参数中,修改Content-Type
axios.get('请求地址',{params: { //body参数id:1, //如:key:id value:1;name:'joe' //key2:name value:'joe'},headers:{ //头部参数token:self.vistorToken}
})
.then(function (response) { //请求成功,response接收返回参数console.log(response);})
.catch(function (error) { //请求失败,error接收失败原因console.log(error);
})
post请求
‘Content-Type’: ‘application/json’
axios.post('请求地址',{ //body参数cityCode:0,City:fCity,channelNo:'motuWebsite',predictPrice:self.DPredictPrice,phone:self.tele}, { headers: { //头部参数'Content-Type': 'application/json',token:sessionStorage.getItem('token')}}
)
.then(function (response) { //请求成功console.log(response);
}).catch(function (error) { //请求失败console.log(error);
});
‘Content-Type’: ‘multipart/form-data’
//构造body参数
let params = new FormData()
params.append('Travel', self.Travel)
params.append('City', fCity)
params.append('Discount', 1)
axios.post('请求地址',params,{headers: { //头部参数'Content-Type': 'multipart/form-data'}})
.then(function (response) { //请求成功console.log(response);
})
.catch(function (error) { //请求失败console.log(error);
});