Simple packaging axios

import axios from "axios";
import qs from "qs";
const server = axios.create({
//baseURL:'',
timeout:5000,
withCredentials:true
})

// request interceptor
server.interceptors.request.use (config => {
IF (config.method == "GET") {
// pass data to the params
config.params config.data = {...};
IF the else} (config.method == "POST") {
// for serializing be compatible config
// config.headers [ "content-type" ] = "application / x-www-form-urlencoded" // data the sequence of values submitted post similar results in this transmission & Key = Val = Key Val
//config.data = qs.stringify (config.data);
}

// sends the processed to a process server
return config;
}, (ERR) => {
Promise.reject (ERR);
})


// intercepted response
server.interceptors.response.use (RES => {
IF (res.statusText == "the OK") {// when intercepted data when res.statusText == "OK", returns only data
return RES. Data;
}
}, (ERR) => {
Promise.reject (ERR);
})

 

export default (method,url,data)=>{
if(method.toUpperCase() == "GET"){
return server.get(url,{
params:data
})
}else if(method.toUpperCase() == "POST"){
return server.post(url,data)
}
}

 

Guess you like

Origin www.cnblogs.com/superclound/p/11261525.html