vue-axios配置
一、安装axios
npm install axios -s
二、添加配置文件,自定义axios实例
import axios from "axios";
// Full config: https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let config = {
// baseURL: process.env.baseURL || process.env.apiUrl || "",
// timeout: 60 * 1000, // Timeout
// withCredentials: true, // Check cross-site Access-Control
baseURL: "http://localhost:8888",
timeout: 3000,
};
const request = axios.create(config);
//--------------------发送请求拦截器:可以在请求发送之前对请求做一些处理.
request.interceptors.request.use(
(config) => {
config.headers["Content-Type"] = "application/json;charset=utf-8";
// config.headers['token'] = user.token; // 设置请求头
return config;
},
(error) => {
return Promise.reject(error);
}
);
//--------------------响应拦截器:可以在接口响应后统一对返回结果进行处理.
// Add a response interceptor
request.interceptors.response.use(
(response) => {
let res = response.data;
return res;
},
function (error) {
// Do something with response error
return Promise.reject(error);
}
);
export default request;
版权声明:本文为weixin_43352606原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。