使用http-proxy-middleware 代理跨域


使用http-proxy-middleware 代理跨域

例如請求的url:“http://f.apiplus.cn/bj11x5.json

1、打開config/index.js,在proxyTable中添寫如下代碼:

proxyTable: { 
  '/api': {  //使用"/api"來代替"http://f.apiplus.c" 
    target: 'http://f.apiplus.cn', //源地址 
    changeOrigin: true, //改變源 
    pathRewrite: { 
      '^/api': 'http://f.apiplus.cn' //路徑重寫 
      } 
  } 
}

2、使用axios請求數據時直接使用“/api”:

getData () { 
 axios.get('/api/bj11x5.json', function (res) { 
   console.log(res) 
 })

通過這中方法去解決跨域,打包部署時還按這種方法會出問題。解決方法如下:

let serverUrl = '/api/'  //本地調試時 
// let serverUrl = 'http://f.apiplus.cn/'  //打包部署上線時 
export default { 
  dataUrl: serverUrl + 'bj11x5.json' 
}

 

 

 

轉載自:https://www.cnblogs.com/ldlx-mars/p/7816316.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM