axios (get post请求、头部参数添加)傻瓜式入门axios

傻瓜式入门,简单粗暴说用法

通过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);
});

 

posted @ 2021-07-14 18:38  web_cnblogs  阅读(1381)  评论(0)    收藏  举报