vue项目中的请求发送,自己创建一个axios实例
request.js
import axios from 'axios';
import store from "../store/index"
import baseURL from '@/src/api/baseUrl'
const service = axios.create({
//正式
baseURL,
timeout: 5000 * 10
});
service.interceptors.request.use(
config => {
config.headers.Authorization = store.state.token //请求头加上token
return config;
},
error => {
return Promise.reject(error);
}
);
service.interceptors.response.use(
response => {
if (response.status === 200) {
return response.data;
} else {
return Promise.reject();
}
},
error => {
return Promise.reject(error.response);
}
);
export default service;