一.axios安装与引用
1.axios的安装
npm install axios --save-dev
2.axios的引用
import axios from 'axios';
二.axios使用
1.GET方法
axios({
method:'get',
url:resquestURL,
data:params
}).then(function(res){
console.log(res.data);
}.bind(this)).catch(function(err){
console.log(err);
})
2.POST方法
axios-post请求需要将提交data转换成键值对的数据格式,借助qs模块(需npm下载)封装提交的data数据
import Qs from 'qs'
var params = {
'api':'json',
'isSubmit':'1'
};
axios({
method:'post',
url:resquestURL,
data:Qs.stringify(params),
header:{
"Content-type":"application/x-www-form-urlencoded"
}
}).then(function(res){
console.log(res.data);
}.bind(this)).catch(function(err){
console.log(err);
})