VUE学习笔记(十四)-调整axios拦截器
调整axios拦截器
src/api/api_config.js
import axios from "axios"; import { getToken } from "@/auth/auth.service"; import { ElMessage } from 'element-plus' axios.defaults.baseURL = "http://localhost:8080/api"; axios.defaults.headers["X-Requested-With"] = "XMLHttpRequest"; axios.defaults.headers.post["Content-Type"] = "application/json"; axios.interceptors.request.use((options) => { //为请求添加token const jwtToken = getToken(); if (jwtToken) { options.headers.Authorization = `Bearer ${jwtToken}`; } return options; }); //响应拦截 axios.interceptors.response.use( (res) => { //响应成功的情况下 console.log(res); return res; }, (error) => { //响应失败的情况下 // console.log(error.response.data.message) ElMessage({ message: error.response.data.message, type: "error", }); return error; } ); export default axios;