vue-cli開發環境實現跨域請求

前端開發時,請求後臺接口經常需要跨域,vue-cli實現跨域請求只需要打開config/index.js,修改如下內容即可。

//例如要請求的接口url爲http://172.3.2.1:8000/look/1

module.exports = {
    dev:{
        proxyTable:{
            '/api':{
                target: 'http://172.3.2.1:8000',
                changeOrigin: true,
                pathRewrite: {
                  '^/api': ''
                }
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

這時在你想請求接口的url處,輸入/api/look/1 即可實現跨域請求。

這時如果打開F12會發現請求的url是localhost:8080/api/look/1,這其實是虛擬從本地請求數據,這樣就不會有跨域的問題產生了。

一般情況下上面的方法是沒有問題的,要是上面的方法行不通,可以試試這樣寫:

//例如要請求的接口url爲http://172.3.2.1:8000/look/1

module.exports = {
    dev:{
        proxyTable:{
            '/look':{
                target: 'http://172.3.2.1:8000',
                changeOrigin: true,
                pathRewrite: {
                  '^/look': '/look'
                }
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

這時在你想請求接口的url處,輸入/look/1 即可實現跨域請求。


轉載自:https://blog.csdn.net/buppt/article/details/78653840

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章