前后端分离如果涉及到Cookie还是免不了服务代理
Proxy代理配置
vite.config.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| export default defineConfig({ plugins: [vue()], resolve: { alias: { '@': '/src', }, }, server: { host: '0.0.0.0', open: false, proxy: { '/api': { target: 'http://127.0.0.1:8000/', changeOrigin: true, rewrite: path => path.replace('/api/', '/'), secure: false, cookieDomainRewrite: '', }, }, }, })
|
同时在我们之前创建的封装了axios的js文件
1 2 3 4 5 6 7 8 9
| import axios from "axios";
const httptool = axios.create({ baseURL: '/api', timeout: 1000, headers: {'X-Custom-Header': 'foobar'} });
|
将baseURL
修改成下面的,是我们每次通过axios请求的url额外添加/api
,以避免和正常的前端路由混淆
然后一般的请求url就不必向之前写的那么长,当然,单独设置baseURL
也是能够做得到
1 2 3 4 5 6 7
| httptool.post('/authorizations/', { username: this.username, password: this.password }, { responseType: 'json', })
|
也不必写
withCredentials: true