//在vite.config。js中,同插件配置同级进行配置
server:{
proxy:{
"/myrequest":{//代理域名,可自行修改
target:"https://m.wzj.com/",//访问服务器的目标域名
changeOrigin:true,//允许跨域
configure:(proxy,options) => {
proxy.on("proxyReq",(proxyReq,req,res) => {
proxyReq.setHeader("Referer","https://m.wzj.com/mall");//设置访问服务器的目标请求头
});
},
rewrite:(path)=>path.replace(/^\/myrequest/,"")//替换代理域名为""
},
},
},
2.在发请求的位置使用自己配置的代理服务器的域名进行访问
import axios from "axios";
// https://m.wzj.com/proxy/articles/featured/list?limit=100&offset=0
export const homeData=()=>{
let url='myrequest/proxy/articles/featured/list?limit=10&offset=0'
axios.get(url).then((result)=>{
return result
})
}