import { Notification } from 'element-ui' //提示后端接口
function handleReponse(response) {
switch (parseInt(response.data.code)) {
case 0:
break
case 404:
Notification.error({
title: 'error',
message: '后端接口网址未找到, 错误代码404',
duration: 5000,
})
break
default:
Notification({
title: 'error',
message: '未知错误,错误代码:' + response.data.code,
duration: 5000,
})
}
return response
}
//响应拦截器
request.interceptors.response.use(
(response) => {
Nprogress.done()
return handleReponse(response)
},
(error) => {
Nprogress.done()
return Promise.reject(error)
}
)
proxy: {
//代理服务器访问目标服务器,解决开发过程中跨域问题
'/api': {
target: 'http://cotton.hzau.edu.cn:80/web',
ws: true,
changeOrigin: true,
pathRewrite: {
'^/api': '/',
},
},
},
2在.env.development
中配置代理服务器地址
3在request.js
文件中配置axios请求地址
import axios from 'axios'
import Nprogress from 'nprogress'
import { Notification } from 'element-ui' //提示后端接口
Nprogress.configure({ showSpinner: false })
const request = axios.create({
baseURL: process.env.VUE_APP_Proxy, //向代理服务器发送请求
timeout: 12000,
headers: {
'content-Type': 'application/json',
Accept: 'application/json',
},
})