【Vue】axios post提交请求转为form data

axios.js

import axios from 'axios';
import qs from 'qs';

// axios 配置
axios.defaults.timeout = 8000;
//配置请求头
// axios.defaults.headers = {'Content-Type': 'application/json;charset=UTF-8'};
axios.defaults.headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'};
//axios.defaults.baseURL = LINKS.BASE;
// axios.defaults.baseURL = getBaseUrl(window.location.href);

//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use(
    config => {

        // config.headers.Authorization = 'Bearer eyJhbGciOiJIUzUxMiJ9';
        if (config.method  === 'post') {
            config.data = qs.stringify(config.data);
        }
        return config;
    },
    error =>{
        return Promise.reject(error);
    }
);

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
    res =>{
        //对响应数据做些事
        if (!res.data.success) {
            return Promise.resolve(res);
        }
        return res;
    },
    error => {
        return Promise.reject(error);
    }
);

export default axios;

 

posted @ 2019-10-01 12:33    阅读(6131)  评论(0编辑  收藏  举报