自学内容网 自学内容网

VUE:跨域配置代理服务器

//在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
    })
}


原文地址:https://blog.csdn.net/SOOOOOF/article/details/140463245

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!