//axios的Get方法封装
function GET(url, data, method) {
var tk = getCookie(‘token’);
if (tk) {
axios({
url: url,
method: ‘get’,
data: data,
headers: { ‘token’: tk }
}).then(function (response) {
if (response.data.ret == -2)//没有访问权限
{
location.href = ‘/’;
}
else if (response.data.ret == -1) {//程序错误
console.log(response.data.msg);
}
else {
method(response.data);
}
}).catch(function (error) {
console.log(error);
})
}
//axios的Post方法封装
function POST(url, data, method) {
var tk = getCookie(‘token’);
if (tk) {
axios({
url: url,
method: ‘post’,
data: data,
headers: { ‘token’: tk }
}).then(function (response) {
if (response.data.ret == -2)//没有访问权限
{
location.href = ‘/’;
}
else if (response.data.ret == -1) {//程序错误
console.log(response.data.msg);
}
else {
method(response.data);
}
}).catch(function (error) {
console.log(error);
})
}
}
POST请求:
axios.post(‘http://api.abc.com/api/user’,{
name:‘frank’,
sex:‘1’
}).then(function (response){
console.log(response.data)
}).catch(function(error){
console.log(error);
});
get请求:
axios.get(‘http://api.abc.com/api/user’,{
param:{Id:132}
}).then(function(response){
console.log(response.data);
}).catch(function(error){
console.log(error);