axois封装

import $axios from 'axios';

$axios.defaults.timeout = 5000;
$axios.defaults.baseURL = "http://127.0.0.1:8888/api";
$axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$axios.interceptors.request.use(config => {
    let token = sessionStorage.getItem("token");
    if (token) {
        config.headers.token = token;
    }
    return config;
})

export const get = (url, data) => {
    return new Promise((resolve, reject) => {
        $axios.get(url, {params: data}).then(data => {
            resolve(data.data);
        }).catch(err => {
            reject(err)
        })
    })
}
export const post = (url, data = null) => {
    return new Promise((resolve, reject) => {
        $axios.get(url, data).then(data => {
            resolve(data.data);
        }).catch(err => {
            reject(err)
        })
    })
}
posted @ 2020-09-23 23:01  星时代曹波涛  阅读(115)  评论(0编辑  收藏  举报