[Vue] axios post into submitting a request 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);
        }
        config return; 
    },
    error => { 
        return Promise.reject (error); 
    } 
); 

// return a status determination (addition response blocker) 
axios.interceptors.response.use ( 
    RES => { 
        // do something in response to the data 
        if ( ! res.data.success) { 
            return Promise.resolve (RES); 
        } 
        return RES; 
    }, 
    error => { 
        return Promise.reject (error); 
    } 
); 

Export default Axios;

 

Guess you like

Origin www.cnblogs.com/jxd283465/p/11615166.html