在项目根目录中找到vite.config.ts文件,配置以下代理:
```
export default defineConfig({
server: {
proxy: {
'/page': {
target: 'http://www.baidu.com/api/', // 目标服务器地址
changeOrigin: true, // 启用代理时,改变源地址
headers: {
"Authorization": "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAi" //设置请求报头
}
}
}
},
})
```
调用方法:
```
axios.post('http://localhost:5173/page', {
headers: {
"Content-Type": "application/json",
}
})
.then((data: any) => {
console.log(data);
})
```
相当于请求了后端服务接口:http://www.baidu.com/api/page