首先我們在本地開發,域名都是localhost,當我們需要請求后台數據時,就會出現跨域的問題
下面就是在vue.config.js配置文件里:
devServer: {
proxy: {
// detail: https://cli.vuejs.org/config/#devserver-proxy
'/api'
: {
target: `http:
//10.24.4.214:8098/api`,
changeOrigin:
true
,
pathRewrite: {
'^/api'
:
''
}
}
}
}
/api表示需要去匹配請求時的url,然后替換成target的值
比如你頁面里是寫的:
axios.post('/api/list/gd')
最終node去請求后台的地址是:http://10.24.4.214:8098/api/list/gd
但是你在瀏覽器里看到的還是:http://localhost:8888/api/list/gd,這時候就不存在跨越的問題的,node服務已經代理拿到數據了