Vue 封装 axios 请求时的坑(Cannot use ‘in’ operator to search for ‘validateStatus’ in http://localhost:8080)
问题重现
mergeConfig.js:60 Uncaught TypeError: Cannot use ‘in’ operator to search for ‘validateStatus’ in http://localhost:8080
at mergeDirectKeys (mergeConfig.js:60:9)
at computeConfigValue (mergeConfig.js:101:25)
at Object.forEach (utils.js:255:10)
at mergeConfig (mergeConfig.js:99:9)
at Function.create (axios.js:40:27)
at request.js:6:24
// 导入 axios 依赖
import axios from 'axios';
// 定义baseUrl
const baseURL = 'http://localhost:8080';
// 创建实例
const instance = axios.create(baseURL);
// 添加响应拦截器
instance.interceptors.response.use(
result => {
return result.data;
},
err => {
alert("服务异常");
return Promise.reject(err);
}
)
export default instance;
解决方法
// 导入 axios 依赖
import axios from 'axios';
// 定义baseUrl
const baseURL = 'http://localhost:8080';
// 创建实例
const instance = axios.create({
baseURL: baseURL
});
// 添加响应拦截器
instance.interceptors.response.use(
result => {
return result.data;
},
err => {
alert("服务异常");
return Promise.reject(err);
}
)
export default instance;
- 正常访问