MENU

axios设置 Auth 请求头

June 3, 2020 • Read: 243 • Vue阅读设置

axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

示例代码:

// 新创建 axios 实例配置
const $axios = axios.create({
    baseURL: 'http://domain.com',
    timeout: 5000,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'sessionId':  Lockr.get("sessionId"),
        'authKey': Lockr.get("authKey"),
    }
});

// 初始化默认post header
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';