vue+axios跨域解決方案

來源:互聯網
上載者:User

標籤:.com   調試   跨域   proxy   alt   url   serve   hang   .json   

通過這種方法也可以解決跨域的問題。

 

使用http-proxy-middleware 代理解決(項目使用vue-cli腳手架搭建)

 

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

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

     

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

2、使用axios請求資料時直接使用“/api”:

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

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

12345 let serverUrl = ‘/api/‘  //本地調試時 // let serverUrl = ‘http://f.apiplus.cn/‘  //打包部署上線時 export default {   dataUrl: serverUrl + ‘bj11x5.json‘ }
調試時定義一個serverUrl來替換我們的“/api”,最後打包時,只需要將“http://www.xxx.com”替換這個“/api”就可以了。 原網址:https://www.cnblogs.com/wangyongcun/p/7665687.html

vue+axios跨域解決方案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.