赞
踩
import axios from 'axios'; // http request 拦截器 const httpService = axios.create({ baseURL: "", // 需自定义 // url前缀-'http:xxx.xxx' timeout: 900000, // 需自定义 请求超时时间 十五分钟 withCredentials: false, // `withCredentials` 表示跨域请求时是否需要使用凭证 }); // request拦截器 httpService.interceptors.request.use( config => { config.headers['Content-Type'] = 'application/json;charset=utf-8'; let user = localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):null if (user) { config.headers['token'] = user.token // 让每个请求携带自定义token 请根据实际情况自行修改 } return config; }, error => { // 请求错误处理 return Promise.reject(error); } ) httpService.interceptors.response.use( response => { if (response.data.errno === 999) { router.replace('/'); console.log("token过期"); } return response; }, error => { return Promise.reject(error); } ); export default httpService;
main.js
import axios from './axios/http.js'
// axios.interceptors.request.use( config => {
// // console.log("config:", config);
// config.headers.Authorization = window.sessionStorage.getItem("Token");
// return config;
// });
Vue.prototype.axios = axios
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。